Conversion to JSON failure
Description of the issue Any circuit with the inverse QubitPermutation gate added will fail if it is loaded from JSON. How to reproduce the issue
import cirq as c
qubits = c.LineQubit.range(3)
circuit = c.Circuit()
circuit.append(c.QubitPermutationGate(permutation=[0,1,2])(qubits[0], qubits[1], qubits[2])**-1)
print(circuit)
json_text = c.to_json(circuit)
print(json_text)
circuit = c.read_json(json_text=json_text)
print(circuit)
0: ───[0>0]──────
│
1: ───[1>1]──────
│
2: ───[2>2]^-1───
{
"cirq_type": "Circuit",
"moments": [
{
"cirq_type": "Moment",
"operations": [
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "_InverseCompositeGate"
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 0
},
{
"cirq_type": "LineQubit",
"x": 1
},
{
"cirq_type": "LineQubit",
"x": 2
}
]
}
]
}
]
}
Traceback (most recent call last):
File "/Users/xxx/json_failure.py", line 11, in <module>
circuit = c.read_json(json_text=json_text)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/protocols/json_serialization.py", line 561, in read_json
return json.loads(json_text, object_hook=obj_hook)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 359, in loads
return cls(**kw).decode(s)
^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/protocols/json_serialization.py", line 352, in __call__
cls = factory_from_json(cirq_type, resolvers=self.resolvers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/protocols/json_serialization.py", line 431, in factory_from_json
raise ValueError(f"Could not resolve type '{type_str}' during deserialization")
ValueError: Could not resolve type '_InverseCompositeGate' during deserialization
Cirq version
You can get the cirq version by printing cirq.__version__. From the command line:
Cirq: 1.4.1
Discussed during bit.ly/cirq-cynq today (2024-10-16): the conclusion is Cirq slhould either fully support the serialization or raise an error. Fully supporting it would is the preferred outcome.
Hi all, I'm still in process learning this repo, but looks like this is low-hanging and not yet assigned. I've made the following change that fix the serialization behavior of the given snippet: https://github.com/quantumlib/Cirq/compare/main...justinpan0:Cirq:main?expand=1. Will create an PR if no immediate feedbacks, thanks!