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

XXPlusYYGate.to_matrix() fails after qasm2 roundtrip #12100

Closed
garrison opened this issue Mar 28, 2024 · 2 comments · Fixed by #12119
Closed

XXPlusYYGate.to_matrix() fails after qasm2 roundtrip #12100

garrison opened this issue Mar 28, 2024 · 2 comments · Fixed by #12119
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export

Comments

@garrison
Copy link
Member

garrison commented Mar 28, 2024

Environment

  • Qiskit version: 1.0.2
  • Python version: 3.11.0
  • Operating system: Linux

What is happening?

After a qasm2 round-trip, a XXPlusYYGate's to_matrix() method no longer works.

This problem seems somewhat special to XXPlusYYGate; it does not exist for most other two-qubit gates we have tried.

I've been able to reproduce this problem as well for CSGate, but not CXGate or CZGate.

How can we reproduce the issue?

from qiskit import qasm2, QuantumCircuit
from qiskit.circuit.library import XXPlusYYGate

# Build a circuit with a single XXPlusYYGate
qc = QuantumCircuit(2)
qc.append(XXPlusYYGate(0.1), [0, 1])

# Perform a qasm2 round trip
program = qasm2.dumps(qc)
qc2 = QuantumCircuit.from_qasm_str(program)

# Call to_matrix on original circuit instructions (works)
for inst in qc.data:
    inst.operation.to_matrix()

# Call to_matrix on circuit instructions after serialization (fails)
for inst in qc2.data:
    inst.operation.to_matrix()

throws the exception

File ~/serverless/.direnv/python-3.11.0/lib/python3.11/site-packages/qiskit/circuit/gate.py:63, in Gate.to_matrix(self)
     61 if hasattr(self, "__array__"):
     62     return self.__array__(dtype=complex)
---> 63 raise CircuitError(f"to_matrix not defined for this {type(self)}")

CircuitError: "to_matrix not defined for this <class 'qiskit.qasm2.parse._DefinedGate'>"

What should happen?

it should work the same as the circuit that did not undergo a round trip.

Any suggestions?

No response

@garrison garrison added the bug Something isn't working label Mar 28, 2024
@jakelishman jakelishman added the mod: qasm2 Relating to OpenQASM 2 import or export label Apr 2, 2024
@jakelishman
Copy link
Member

jakelishman commented Apr 2, 2024

Thanks for the report. This happens because it's quite tricky for the OQ importers to map OQ gate declarations (for things outside qelib1.inc) to Qiskit gates, so it uses a custom qasm2._DefinedGate class to represent things built by gate. We can give that a to_matrix method that just calls Operator(self.definition).data and it should all work the way you like (but won't be as efficient as the original's to_matrix() method - not much we can do about that, though).

In the immediate term, you can replace Gate.to_matrix with a call to quantum_info.Operator(op).data, since Operator will fall back to constructing the matrix from the gate definition if it doesn't have a to_matrix, or you can use the custom_instructions field of qasm2.loads to link the name xx_plus_yy to XXPlusYYGate in Qiskit, so the importer will actually use the proper gate.

I'm still hoping to improve the story around gate imports and exports in OQ2 and OQ3 (see #10737), but it's hard to know when exactly we'll have time and priority to do it.

@garrison
Copy link
Member Author

garrison commented Apr 2, 2024

In the immediate term, you can replace Gate.to_matrix with a call to quantum_info.Operator(op).data, since Operator will fall back to constructing the matrix from the gate definition if it doesn't have a to_matrix

Thanks, Jake. This sounds like a reasonable workaround. We will try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants