diff --git a/cirq/json_resolver_cache.py b/cirq/json_resolver_cache.py index 44641c392df..014339243ea 100644 --- a/cirq/json_resolver_cache.py +++ b/cirq/json_resolver_cache.py @@ -156,5 +156,8 @@ def two_qubit_matrix_gate(matrix): 'sympy.Float': lambda approx: sympy.Float(approx), 'sympy.Integer': sympy.Integer, 'sympy.Rational': sympy.Rational, + 'sympy.pi': lambda: sympy.pi, + 'sympy.E': lambda: sympy.E, + 'sympy.EulerGamma': lambda: sympy.EulerGamma, 'complex': complex, } diff --git a/cirq/protocols/json_serialization.py b/cirq/protocols/json_serialization.py index 8f4274f41af..77bc8d8efaf 100644 --- a/cirq/protocols/json_serialization.py +++ b/cirq/protocols/json_serialization.py @@ -235,6 +235,7 @@ def default(self, o): # Sympy object? (Must come before general number checks.) # TODO: More support for sympy # Github issue: https://github.com/quantumlib/Cirq/issues/2014 + if isinstance(o, sympy.Symbol): return obj_to_dict_helper(o, ['name'], namespace='sympy') @@ -254,6 +255,18 @@ def default(self, o): 'q': o.q, } + if isinstance(o, sympy.NumberSymbol): + # check if `o` is a numeric symbol, + # i.e. one of the transcendental numbers + # sympy.pi, sympy.E or sympy.EulerGamma + # (note that these are singletons). + if o is sympy.pi: + return {'cirq_type': 'sympy.pi'} + if o is sympy.E: + return {'cirq_type': 'sympy.E'} + if o is sympy.EulerGamma: + return {'cirq_type': 'sympy.EulerGamma'} + # A basic number object? if isinstance(o, numbers.Integral): return int(o) diff --git a/cirq/protocols/json_serialization_test.py b/cirq/protocols/json_serialization_test.py index c795083638d..5564da61abd 100644 --- a/cirq/protocols/json_serialization_test.py +++ b/cirq/protocols/json_serialization_test.py @@ -295,6 +295,10 @@ def test_sympy(): assert_json_roundtrip_works(t * 2) assert_json_roundtrip_works(4 * t + 3 * s + 2) + assert_json_roundtrip_works(sympy.pi) + assert_json_roundtrip_works(sympy.E) + assert_json_roundtrip_works(sympy.EulerGamma) + class SBKImpl(cirq.SerializableByKey): """A test implementation of SerializableByKey.""" diff --git a/cirq/protocols/json_test_data/sympy.E.json b/cirq/protocols/json_test_data/sympy.E.json new file mode 100644 index 00000000000..3736998867a --- /dev/null +++ b/cirq/protocols/json_test_data/sympy.E.json @@ -0,0 +1,5 @@ +[ + { + "cirq_type": "sympy.E" + } +] diff --git a/cirq/protocols/json_test_data/sympy.E.repr b/cirq/protocols/json_test_data/sympy.E.repr new file mode 100644 index 00000000000..75caebd21a0 --- /dev/null +++ b/cirq/protocols/json_test_data/sympy.E.repr @@ -0,0 +1,3 @@ +[ +sympy.E +] \ No newline at end of file diff --git a/cirq/protocols/json_test_data/sympy.EulerGamma.json b/cirq/protocols/json_test_data/sympy.EulerGamma.json new file mode 100644 index 00000000000..4dac17aaf7f --- /dev/null +++ b/cirq/protocols/json_test_data/sympy.EulerGamma.json @@ -0,0 +1,5 @@ +[ + { + "cirq_type": "sympy.EulerGamma" + } +] diff --git a/cirq/protocols/json_test_data/sympy.EulerGamma.repr b/cirq/protocols/json_test_data/sympy.EulerGamma.repr new file mode 100644 index 00000000000..8c08a7f87e8 --- /dev/null +++ b/cirq/protocols/json_test_data/sympy.EulerGamma.repr @@ -0,0 +1,3 @@ +[ +sympy.EulerGamma +] \ No newline at end of file diff --git a/cirq/protocols/json_test_data/sympy.pi.json b/cirq/protocols/json_test_data/sympy.pi.json new file mode 100644 index 00000000000..94053e6a717 --- /dev/null +++ b/cirq/protocols/json_test_data/sympy.pi.json @@ -0,0 +1,5 @@ +[ + { + "cirq_type": "sympy.pi" + } +] diff --git a/cirq/protocols/json_test_data/sympy.pi.repr b/cirq/protocols/json_test_data/sympy.pi.repr new file mode 100644 index 00000000000..7a1027efb2d --- /dev/null +++ b/cirq/protocols/json_test_data/sympy.pi.repr @@ -0,0 +1,3 @@ +[ +sympy.pi +] \ No newline at end of file