Skip to content

Commit 456ae6a

Browse files
hvenev-insaittgehr
andcommitted
Tolerate qiskit.circuit.Gate.to_matrix() failures
Co-authored-by: Timon Gehr <timon.gehr@gmx.ch> Reported-by: Timon Gehr <timon.gehr@gmx.ch>
1 parent 25e7470 commit 456ae6a

3 files changed

Lines changed: 59 additions & 12 deletions

File tree

python/qblaze/qiskit.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,16 @@ def handle_generic_uncontrolled(self, sim: Simulator, ctl: list[tuple[int, bool]
426426
self.sv.append((op, sv))
427427
return
428428

429-
match op:
430-
case qiskit.circuit.Gate() if op.num_qubits == 1:
431-
assert not clbits
432-
[q] = qubits
433-
(theta, phi, lam) = _zyz_decomposer.angles(op.to_matrix())
429+
if isinstance(op, qiskit.circuit.Gate) and op.num_qubits == 1:
430+
assert not clbits
431+
[q] = qubits
432+
try:
433+
m = op.to_matrix()
434+
except qiskit.circuit.exceptions.CircuitError:
435+
# Qiskit...
436+
pass
437+
else:
438+
(theta, phi, lam) = _zyz_decomposer.angles(m)
434439
return sim.u3(q, theta, phi, lam)
435440

436441
if (sub := op.definition) is not None:
@@ -439,13 +444,18 @@ def handle_generic_uncontrolled(self, sim: Simulator, ctl: list[tuple[int, bool]
439444

440445
@_CONTROLLED.register(qiskit.circuit.Instruction)
441446
def handle_generic_controlled(self, sim: Simulator, ctl: list[tuple[int, bool]], op: qiskit.circuit.Instruction, clbits: list[qiskit.circuit.Clbit], qubits: list[int], /) -> None:
442-
match op:
443-
case qiskit.circuit.Gate() if op.num_qubits == 1:
444-
assert not clbits
445-
[q] = qubits
446-
subctl = [*ctl, (q, True)]
447-
# rz(lam); h; rz(theta); h; rz(phi)
448-
(theta, phi, lam, corr) = _zxz_decomposer.angles_and_phase(op.to_matrix())
447+
if isinstance(op, qiskit.circuit.Gate) and op.num_qubits == 1:
448+
assert not clbits
449+
[q] = qubits
450+
subctl = [*ctl, (q, True)]
451+
# rz(lam); h; rz(theta); h; rz(phi)
452+
try:
453+
m = op.to_matrix()
454+
except qiskit.circuit.exceptions.CircuitError:
455+
# Qiskit...
456+
pass
457+
else:
458+
(theta, phi, lam, corr) = _zxz_decomposer.angles_and_phase(m)
449459
corr -= (theta + phi + lam) / 2
450460
if abs(theta) < 1e-8:
451461
sim.mcphase(subctl, lam + phi)

tests/circuits/qasm3-gate.qasm3

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
OPENQASM 3.0;
2+
include "stdgates.inc";
3+
gate phxz(a,x,z) q0 {
4+
rz(-a*pi) q0;
5+
rx(x*pi) q0;
6+
rz((a+z)*pi) q0;
7+
}
8+
qreg q[1];
9+
phxz(0.7070988294,0.1093253464,0.07720078801) q[0];

tests/test_qasm.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import sys
3+
4+
from qiskit import qasm3
5+
import qblaze.qiskit
6+
7+
import pytest
8+
9+
10+
circ_path = os.path.dirname(__file__) + '/circuits'
11+
qasm3_circuits = []
12+
13+
for name in os.listdir(circ_path):
14+
if name.startswith('.'):
15+
continue
16+
if name.endswith('.qasm3'):
17+
with open(f'{circ_path}/{name}', 'r') as f:
18+
qasm3_circuits.append(f.read())
19+
continue
20+
raise RuntimeError(f'Bad file {name!r}')
21+
22+
23+
@pytest.mark.parametrize('circ', qasm3_circuits)
24+
def test_qasm3(circ: str) -> None:
25+
circ = qasm3.loads(circ)
26+
backend = qblaze.qiskit.Backend()
27+
result = backend.run(circ, shots=128).result()
28+
result.get_counts()

0 commit comments

Comments
 (0)