From 5ca0b4790907bd3e150929636237e483c05d2b73 Mon Sep 17 00:00:00 2001 From: "Antoine (Tony) Bruguier" Date: Wed, 6 Apr 2022 15:49:29 -0700 Subject: [PATCH] Cirq web supports LineQubits (#5211) This would be to fix #5173 --- cirq-web/cirq_web/circuits/circuit.py | 7 ++++++- cirq-web/cirq_web/circuits/circuit_test.py | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cirq-web/cirq_web/circuits/circuit.py b/cirq-web/cirq_web/circuits/circuit.py index e610253d5a0..348b0913664 100644 --- a/cirq-web/cirq_web/circuits/circuit.py +++ b/cirq-web/cirq_web/circuits/circuit.py @@ -87,5 +87,10 @@ def _build_3D_symbol(self, operation, moment) -> Operation3DSymbol: symbol_info = resolve_operation(operation, self._resolvers) location_info = [] for qubit in operation.qubits: - location_info.append({'row': qubit.row, 'col': qubit.col}) + if isinstance(qubit, cirq.GridQubit): + location_info.append({'row': qubit.row, 'col': qubit.col}) + elif isinstance(qubit, cirq.LineQubit): + location_info.append({'row': qubit.x, 'col': 0}) + else: + raise ValueError('Unsupported qubit type') return Operation3DSymbol(symbol_info.labels, location_info, symbol_info.colors, moment) diff --git a/cirq-web/cirq_web/circuits/circuit_test.py b/cirq-web/cirq_web/circuits/circuit_test.py index 5853576b700..97b9f1815fc 100644 --- a/cirq-web/cirq_web/circuits/circuit_test.py +++ b/cirq-web/cirq_web/circuits/circuit_test.py @@ -14,6 +14,8 @@ import cirq import cirq_web +import pytest + def strip_ws(string): return "".join(string.split()) @@ -28,9 +30,9 @@ def test_circuit_init_type(): assert isinstance(circuit3d, cirq_web.Circuit3D) -def test_circuit_client_code(): - qubits = [cirq.GridQubit(x, y) for x in range(2) for y in range(2)] - moment = cirq.Moment(cirq.H(qubits[0])) +@pytest.mark.parametrize('qubit', [cirq.GridQubit(0, 0), cirq.LineQubit(0)]) +def test_circuit_client_code(qubit): + moment = cirq.Moment(cirq.H(qubit)) circuit = cirq_web.Circuit3D(cirq.Circuit(moment)) circuit_obj = [ @@ -64,6 +66,14 @@ def test_circuit_client_code(): assert strip_ws(circuit.get_client_code()) == strip_ws(expected_client_code) +def test_circuit_client_code_unsupported_qubit_type(): + moment = cirq.Moment(cirq.H(cirq.NamedQubit('q0'))) + circuit = cirq_web.Circuit3D(cirq.Circuit(moment)) + + with pytest.raises(ValueError, match='Unsupported qubit type'): + circuit.get_client_code() + + def test_circuit_default_bundle_name(): qubits = [cirq.GridQubit(x, y) for x in range(2) for y in range(2)] moment = cirq.Moment(cirq.H(qubits[0]))