Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions qsimcirq/qsim_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ def add_op_to_circuit(
"""Adds an operation to a noisy or noiseless circuit."""
qsim_gate = qsim_op.gate
gate_kind = _cirq_gate_kind(qsim_gate)
if gate_kind is None:
raise ValueError("{!r} is not a supported gate.".format(qsim_gate))
qubits = [qubit_to_index_dict[q] for q in qsim_op.qubits]

qsim_qubits = qubits
Expand Down
20 changes: 20 additions & 0 deletions qsimcirq_tests/qsimcirq_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -2196,3 +2196,23 @@
want = np.array([0.0 - 0.5j, 0.0 + 0.5j, 0.0 - 0.5j, 0.0 + 0.5j])
_, res, _ = qsim_sim.simulate_into_1d_array(c)
np.testing.assert_allclose(res, np.array(want, dtype=np.complex64))


def test_add_op_to_circuit_unsupported_gate():
class UnsupportedGate(cirq.Gate):
def _num_qubits_(self) -> int:
return 1

def _unitary_(self):
return np.eye(2)

def __repr__(self):
return "UnsupportedGate()"

q0 = cirq.LineQubit(0)
op = UnsupportedGate().on(q0)
circuit = qsimcirq.qsim.Circuit()
qubit_to_index = {q0: 0}

with pytest.raises(ValueError, match="UnsupportedGate\(\) is not a supported gate."):
qsimcirq.add_op_to_circuit(op, 0, qubit_to_index, circuit)
Loading