Skip to content

Commit

Permalink
Add sympy Serialization (#850)
Browse files Browse the repository at this point in the history
* Added methods to serialize and deserialize quantum data types.

* Add serialization and deserialization methods for all data types. Write
unit tests to ensure round trip of each datatype using both numeric and
sympy inputs.

* Update qualtran/protos/data_types.proto

Co-authored-by: Tanuj Khattar <tanujkhattar@google.com>

* Update Proto:wq

* Fixed error formatting

* Remove bitsize from QBit

* Fixed formatting

* Remove uncessesary files

* Fix data type in formatted string in error message

* Update qualtran/serialization/data_types_test.py

* Added Sympy serialization.

Sympy expressions can now be serialized through a recursive proto.

* Clean up workspace

* Remote large tests from get_bloq_examples()

* Remove qpe_hubbard_model tests

The qpe_hubbard_model is too large to test and causes developer
friction. We have disabled the tests for now

* Remove qpe_hubbard tests

These tests are very large and will cause problems when running. These
tests are now disabled.

* Cleanup branch

* Only remove the serialization tests from the large bloq.s

Move the changes from bloq_finder.py to bloq_report_card.py.  This way,
only the serialization portion of the test is affected.

* Address issues with failing sympy tests.

Added support for fraction constant parameters.
Fixed issue with cwap test assertion which expected sympy in the old
string format.

* Fix formatting

* Fix formatting

* Delete Untitled.ipynb

Delete scratch notebook

* Cleanup after merging local branches.

(Old changes were accidentally left in).

* Remove changes from PR#849

Rather than building on top of #849, we keep the changes separate.

* Fixed assortment of bugs in fraction, and constant symbol serialization

* Scratch test

* Fixed bad reference to resolver dict by bloq module name.

* Minor refactoring

* Ran formatting tools

* Cleanup forgotten test code.

* Add tests, fix naming conventions, and add proper return types.

* Update sympy_test.py

Call sympy serialization directly rather than calling it through bloq.

* Apply suggestions from code review

Addressed nit comments.

Co-authored-by: Tanuj Khattar <tanujkhattar@google.com>

* Fix sympy_test

* Fix return type of sympy_expr_from_proto

---------

Co-authored-by: Tanuj Khattar <tanujkhattar@google.com>
  • Loading branch information
Epsilon1024 and tanujkhattar authored Apr 19, 2024
1 parent 7bf19b3 commit c78dd0b
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 26 deletions.
1 change: 1 addition & 0 deletions dev_tools/qualtran_dev_tools/bloq_report_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def get_bloq_report_card(
bexamples: Optional[Iterable[BloqExample]] = None,
package_prefix: str = 'qualtran.bloqs.',
) -> pd.DataFrame:

if bclasses is None:
bclasses = get_bloq_classes()
if bexamples is None:
Expand Down
1 change: 0 additions & 1 deletion qualtran/protos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

3 changes: 2 additions & 1 deletion qualtran/protos/bloq.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "qualtran/protos/args.proto";
import "qualtran/protos/registers.proto";
import "qualtran/protos/data_types.proto";
import "qualtran/protos/ctrl_spec.proto";
import "qualtran/protos/sympy.proto";

package qualtran;

Expand All @@ -32,7 +33,7 @@ message BloqArg {
double float_val = 3;
string string_val = 4;
// Sympy expression generated using str(expr).
string sympy_expr = 5;
Term sympy_expr = 5;
// N-dimensional numpy array stored as bytes.
NDArray ndarray = 6;
// Integer reference of a subbloq. Assumes access to a BloqLibrary.
Expand Down
35 changes: 18 additions & 17 deletions qualtran/protos/bloq_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions qualtran/protos/bloq_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import qualtran.protos.args_pb2
import qualtran.protos.ctrl_spec_pb2
import qualtran.protos.data_types_pb2
import qualtran.protos.registers_pb2
import qualtran.protos.sympy_pb2
import sys

if sys.version_info >= (3, 8):
Expand Down Expand Up @@ -56,8 +57,9 @@ class BloqArg(google.protobuf.message.Message):
int_val: builtins.int
float_val: builtins.float
string_val: builtins.str
sympy_expr: builtins.str
"""Sympy expression generated using str(expr)."""
@property
def sympy_expr(self) -> qualtran.protos.sympy_pb2.Term:
"""Sympy expression generated using str(expr)."""
@property
def ndarray(self) -> qualtran.protos.args_pb2.NDArray:
"""N-dimensional numpy array stored as bytes."""
Expand Down Expand Up @@ -86,7 +88,7 @@ class BloqArg(google.protobuf.message.Message):
int_val: builtins.int = ...,
float_val: builtins.float = ...,
string_val: builtins.str = ...,
sympy_expr: builtins.str = ...,
sympy_expr: qualtran.protos.sympy_pb2.Term | None = ...,
ndarray: qualtran.protos.args_pb2.NDArray | None = ...,
subbloq: builtins.int = ...,
cirq_json_gzip: builtins.bytes = ...,
Expand Down
75 changes: 75 additions & 0 deletions qualtran/protos/sympy.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

syntax = "proto3";
package qualtran;

// A function sympy expression.
enum Function {
// Each Term has an associated function. A "NONE" function means that the term
// is made up of a single parameter and can not be decomposed further.
NONE = 0;
Add = 1;
Mul = 2;
Pow = 3;
Mod = 4;
Log = 5;
Floor = 6;
Ceiling = 7;
Max = 8;
Min = 9;
Sin = 10;
Cos = 11;
Tan = 12;
}

// Represents a constant, rational number.
message Rational {
Parameter numerator = 1;
Parameter denominator = 2;
}

enum ConstSymbol {
Pi = 0;
E = 1;
EulerGamma = 2;
Infinity = 3;
ImaginaryUnit = 4;
}

// A single parameter of a sympy expression.
message Parameter {
oneof parameter {
int32 const_int = 1;
string symbol = 2;
Rational const_rat = 3;
float const_float = 4;
ConstSymbol const_symbol = 5;

}
}

message Operand {
oneof operand {
Term term = 1;
Parameter parameter = 2;
}
}

message Term {
Function function = 1;
repeated Operand operands = 2;
}
36 changes: 36 additions & 0 deletions qualtran/protos/sympy_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c78dd0b

Please sign in to comment.