You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
fromqiskitimportqasm2, QuantumCircuitfromqiskit.circuit.libraryimportXXPlusYYGate# Build a circuit with a single XXPlusYYGateqc=QuantumCircuit(2)
qc.append(XXPlusYYGate(0.1), [0, 1])
# Perform a qasm2 round tripprogram=qasm2.dumps(qc)
qc2=QuantumCircuit.from_qasm_str(program)
# Call to_matrix on original circuit instructions (works)forinstinqc.data:
inst.operation.to_matrix()
# Call to_matrix on circuit instructions after serialization (fails)forinstinqc2.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
The text was updated successfully, but these errors were encountered:
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.
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.
Environment
What is happening?
After a qasm2 round-trip, a
XXPlusYYGate
'sto_matrix()
method no longer works.This problem seems somewhat special toXXPlusYYGate
; 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 notCXGate
orCZGate
.How can we reproduce the issue?
throws the exception
What should happen?
it should work the same as the circuit that did not undergo a round trip.
Any suggestions?
No response
The text was updated successfully, but these errors were encountered: