Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion to JSON failure #6769

Open
glanzz opened this issue Oct 15, 2024 · 1 comment
Open

Conversion to JSON failure #6769

glanzz opened this issue Oct 15, 2024 · 1 comment
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@glanzz
Copy link

glanzz commented Oct 15, 2024

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
@glanzz glanzz added the kind/bug-report Something doesn't seem to work. label Oct 15, 2024
@NoureldinYosri NoureldinYosri added triage/discuss Needs decision / discussion, bring these up during Cirq Cynque triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Oct 15, 2024
@mhucka
Copy link
Contributor

mhucka commented Oct 16, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

No branches or pull requests

3 participants