quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.26k stars 1.01k forks source link

Conversion to JSON failure #6769

Open glanzz opened 4 days ago

glanzz commented 4 days ago

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)
```bash 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 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
mhucka commented 3 days ago

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.