Skip to content

Commit 087e393

Browse files
gadialjakelishman
andauthored
Fix an error in qasm3 exporter when operating on unitary gates (Qiskit#13633)
* Fix error in qasm3 exporter when operating on unitary gates * Update test/python/qasm3/test_export.py Co-authored-by: Jake Lishman <[email protected]> * Update releasenotes/notes/fix-qasm-3-unitary-2da190be6ba25bbd.yaml Co-authored-by: Jake Lishman <[email protected]> --------- Co-authored-by: Jake Lishman <[email protected]>
1 parent b98e0d0 commit 087e393

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

qiskit/circuit/library/generalized_gates/permutation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def inverse(self, annotated: bool = False) -> PermutationGate:
186186

187187
return PermutationGate(pattern=_inverse_pattern(self.pattern))
188188

189-
def _qasm2_decomposition(self):
189+
def _qasm_decomposition(self):
190190
# pylint: disable=cyclic-import
191191
from qiskit.synthesis.permutation import synth_permutation_basic
192192

qiskit/circuit/library/generalized_gates/unitary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def control(
202202
)
203203
return gate
204204

205-
def _qasm2_decomposition(self):
205+
def _qasm_decomposition(self):
206206
"""Return an unparameterized version of ourselves, so the OQ2 exporter doesn't choke on the
207207
non-standard things in our `params` field."""
208208
out = self.definition.to_gate()

qiskit/qasm2/export.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ def _define_custom_operation(operation, gates_to_define):
314314
# definition, but still continue to return the given object as the call-site object.
315315
if operation.base_class in known_good_parameterized:
316316
parameterized_operation = type(operation)(*_FIXED_PARAMETERS[: len(operation.params)])
317-
elif hasattr(operation, "_qasm2_decomposition"):
318-
new_op = operation._qasm2_decomposition()
317+
elif hasattr(operation, "_qasm_decomposition"):
318+
new_op = operation._qasm_decomposition()
319319
parameterized_operation = operation = new_op.copy(name=_escape_name(new_op.name, "gate_"))
320320
else:
321321
parameterized_operation = operation

qiskit/qasm3/exporter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,13 +1180,16 @@ def build_gate_call(self, instruction: CircuitInstruction):
11801180
11811181
This will also push the gate into the symbol table (if required), including recursively
11821182
defining the gate blocks."""
1183-
ident = self.symbols.get_gate(instruction.operation)
1183+
operation = instruction.operation
1184+
if hasattr(operation, "_qasm_decomposition"):
1185+
operation = operation._qasm_decomposition()
1186+
ident = self.symbols.get_gate(operation)
11841187
if ident is None:
1185-
ident = self.define_gate(instruction.operation)
1188+
ident = self.define_gate(operation)
11861189
qubits = [self._lookup_bit(qubit) for qubit in instruction.qubits]
11871190
parameters = [
11881191
ast.StringifyAndPray(self._rebind_scoped_parameters(param))
1189-
for param in instruction.operation.params
1192+
for param in operation.params
11901193
]
11911194
if not self.disable_constants:
11921195
for parameter in parameters:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Fix a bug in :class:`.qasm3.Exporter` that caused the exporter to crash when
5+
handling a unitary gate due to incorrect processing of its ``params`` field.

test/python/qasm3/test_export.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,23 @@ def test_switch_v1_expr_target(self):
26652665
test = dumps(qc, experimental=ExperimentalFeatures.SWITCH_CASE_V1)
26662666
self.assertEqual(test, expected)
26672667

2668+
def test_circuit_with_unitary(self):
2669+
"""Test that circuits with `unitary` gate are correctly handled"""
2670+
matrix = [[0, 1], [1, 0]]
2671+
qc = QuantumCircuit(1)
2672+
qc.unitary(matrix, [0])
2673+
expected = """\
2674+
OPENQASM 3.0;
2675+
include "stdgates.inc";
2676+
gate unitary _gate_q_0 {
2677+
U(pi, -pi, 0) _gate_q_0;
2678+
}
2679+
qubit[1] q;
2680+
unitary q[0];
2681+
"""
2682+
test = dumps(qc)
2683+
self.assertEqual(test, expected)
2684+
26682685

26692686
@ddt
26702687
class TestQASM3ExporterFailurePaths(QiskitTestCase):

0 commit comments

Comments
 (0)