Skip to content

Commit 003de6b

Browse files
committed
🧪 Add error test for add_op_to_circuit unsupported gate
Added a test case for the ValueError raised when an unsupported gate is passed to add_op_to_circuit. Also improved the library code by adding an explicit check for unsupported gates early in the function.
1 parent b69cd97 commit 003de6b

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

qsimcirq/qsim_circuit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ def add_op_to_circuit(
271271
"""Adds an operation to a noisy or noiseless circuit."""
272272
qsim_gate = qsim_op.gate
273273
gate_kind = _cirq_gate_kind(qsim_gate)
274+
if gate_kind is None:
275+
raise ValueError("{!r} is not a supported gate.".format(qsim_gate))
274276
qubits = [qubit_to_index_dict[q] for q in qsim_op.qubits]
275277

276278
qsim_qubits = qubits

qsimcirq_tests/qsimcirq_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,3 +2196,23 @@ def test_1d_representation():
21962196
want = np.array([0.0 - 0.5j, 0.0 + 0.5j, 0.0 - 0.5j, 0.0 + 0.5j])
21972197
_, res, _ = qsim_sim.simulate_into_1d_array(c)
21982198
np.testing.assert_allclose(res, np.array(want, dtype=np.complex64))
2199+
2200+
2201+
def test_add_op_to_circuit_unsupported_gate():
2202+
class UnsupportedGate(cirq.Gate):
2203+
def _num_qubits_(self) -> int:
2204+
return 1
2205+
2206+
def _unitary_(self):
2207+
return np.eye(2)
2208+
2209+
def __repr__(self):
2210+
return "UnsupportedGate()"
2211+
2212+
q0 = cirq.LineQubit(0)
2213+
op = UnsupportedGate().on(q0)
2214+
circuit = qsimcirq.qsim.Circuit()
2215+
qubit_to_index = {q0: 0}
2216+
2217+
with pytest.raises(ValueError, match="UnsupportedGate\(\) is not a supported gate."):
2218+
qsimcirq.add_op_to_circuit(op, 0, qubit_to_index, circuit)

0 commit comments

Comments
 (0)