Skip to content

Commit

Permalink
patch fixing a serialization issue with rx,ry and rz gates. (#3837)
Browse files Browse the repository at this point in the history
The current serialization of parametrized circuits misses the case where a sympy object is a numeric constant, as for exampole sympy.pi. This problem arises for parametrized rx, ry and rz gates

This patch is an attempt at fixing this.
  • Loading branch information
mganahl authored Apr 1, 2021
1 parent ff8fb57 commit e0365e4
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cirq/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
13 changes: 13 additions & 0 deletions cirq/protocols/json_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions cirq/protocols/json_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
5 changes: 5 additions & 0 deletions cirq/protocols/json_test_data/sympy.E.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"cirq_type": "sympy.E"
}
]
3 changes: 3 additions & 0 deletions cirq/protocols/json_test_data/sympy.E.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
sympy.E
]
5 changes: 5 additions & 0 deletions cirq/protocols/json_test_data/sympy.EulerGamma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"cirq_type": "sympy.EulerGamma"
}
]
3 changes: 3 additions & 0 deletions cirq/protocols/json_test_data/sympy.EulerGamma.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
sympy.EulerGamma
]
5 changes: 5 additions & 0 deletions cirq/protocols/json_test_data/sympy.pi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"cirq_type": "sympy.pi"
}
]
3 changes: 3 additions & 0 deletions cirq/protocols/json_test_data/sympy.pi.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
sympy.pi
]

0 comments on commit e0365e4

Please sign in to comment.