def test_to_json(): dev = PasqalDevice(qubits=[cirq.NamedQubit('q4')]) d = dev._json_dict_() assert d == { "cirq_type": "PasqalDevice", "qubits": [cirq.NamedQubit('q4')] }
def test_init_errors(): line = cirq.devices.LineQubit.range(3) with pytest.raises(TypeError, match="Unsupported qubit type"): _ = PasqalDevice(control_radius=1.5, qubits=line) with pytest.raises(ValueError): _ = PasqalDevice(control_radius=-1, qubits=[ThreeDGridQubit(0, 0, 0)])
def test_value_equal(): dev = PasqalDevice(qubits=[cirq.NamedQubit('q1')]) assert PasqalDevice(qubits=[cirq.NamedQubit('q1')]) == dev dev = PasqalVirtualDevice(control_radius=1.0, qubits=[TwoDQubit(0, 0)]) assert PasqalVirtualDevice(control_radius=1.0, qubits=[TwoDQubit(0, 0)]) == dev
def test_to_json(): dev = PasqalDevice(qubits=[cirq.NamedQubit('q4')]) d = dev._json_dict_() assert d == { "cirq_type": "PasqalDevice", "qubits": [cirq.NamedQubit('q4')] } vdev = PasqalVirtualDevice(control_radius=2, qubits=[TwoDQubit(0, 0)]) d = vdev._json_dict_() assert d == { "cirq_type": "PasqalVirtualDevice", "control_radius": 2, "qubits": [cirq.pasqal.TwoDQubit(0, 0)], }
def test_init_errors(): line = cirq.devices.LineQubit.range(3) with pytest.raises(TypeError, match="Unsupported qubit type"): PasqalDevice(qubits=line) with pytest.raises(ValueError, match="Too many qubits"): generic_device(101) with pytest.raises(TypeError, match="Unsupported qubit type"): PasqalVirtualDevice(control_radius=1.0, qubits=[cirq.NamedQubit('q0')]) with pytest.raises(TypeError, match="All qubits must be of same type."): PasqalVirtualDevice(control_radius=1.0, qubits=[TwoDQubit(0, 0), cirq.GridQubit(1, 0)]) with pytest.raises( ValueError, match="Control_radius needs to be a non-negative float"): square_virtual_device(control_r=-1.0, num_qubits=2) with pytest.raises( ValueError, match="Control_radius cannot be larger than " "3 times the minimal distance between qubits.", ): square_virtual_device(control_r=11.0, num_qubits=2)
def cubic_device(width: int, height: int, depth: int, holes=()) -> PasqalDevice: return PasqalDevice(control_radius=1.5, qubits=[ ThreeDGridQubit(row, col, lay) for row in range(width) for col in range(height) for lay in range(depth) if ThreeDGridQubit(row, col, lay) not in holes ])
def test_pasqal_converter(): q = cirq.NamedQubit.range(2, prefix='q') g = cirq.TwoQubitGate() class FakeOperation(cirq.ops.GateOperation): def __init__(self, gate, qubits): self._gate = gate self._qubits = qubits @property def qubits(self): return self._qubits def with_qubits(self, *new_qubits): return FakeOperation(self._gate, new_qubits) op = FakeOperation(g, q).with_qubits(*q) d = PasqalDevice(q) with pytest.raises(TypeError, match="Don't know how to work with"): d.decompose_operation(op)
def test_get_op_string(): p_qubits = cirq.NamedQubit.range(2, prefix='q') p_device = PasqalDevice(p_qubits) noise_model = PasqalNoiseModel(p_device) circuit = cirq.Circuit() circuit.append(cirq.ops.HPowGate(exponent=0.5).on(p_qubits[0])) with pytest.raises(ValueError, match='Got unknown operation:'): for moment in circuit._moments: _ = noise_model.noisy_moment(moment, p_qubits) with pytest.raises(ValueError, match='Got unknown operation:'): _ = noise_model.get_op_string(circuit)
def test_decompose_error(): d = cubic_device(2, 2, 1, holes=[ThreeDGridQubit(1, 1, 0)]) op = (cirq.ops.CCZ**1.5).on(*(d.qubit_list())) assert d.decompose_operation(op) == [op] op = cirq.H.on(ThreeDGridQubit(0, 0, 0)) decomposition = d.decompose_operation(op) assert len(decomposition) == 2 assert decomposition == [ (cirq.Y**0.5).on(ThreeDGridQubit(0, 0, 0)), cirq.XPowGate(exponent=1.0, global_shift=-0.25).on(ThreeDGridQubit(0, 0, 0)) ] # MeasurementGate is not a GateOperation with pytest.raises(TypeError): d.decompose_operation(cirq.ops.MeasurementGate(num_qubits=1)) # It has to be made into one assert PasqalDevice.is_pasqal_device_op( cirq.ops.GateOperation(cirq.ops.MeasurementGate(1), [ThreeDGridQubit(0, 0, 0)])) assert PasqalDevice.is_pasqal_device_op( cirq.ops.X(ThreeDGridQubit(0, 0, 0)))
def test_NoiseModel_init(): noise_model = PasqalNoiseModel(PasqalDevice([])) assert noise_model.noise_op_dict == { str(cirq.ops.YPowGate()): cirq.ops.depolarize(1e-2), str(cirq.ops.ZPowGate()): cirq.ops.depolarize(1e-2), str(cirq.ops.XPowGate()): cirq.ops.depolarize(1e-2), str(cirq.ops.HPowGate(exponent=1)): cirq.ops.depolarize(1e-2), str(cirq.ops.PhasedXPowGate(phase_exponent=0)): cirq.ops.depolarize(1e-2), str(cirq.ops.CNotPowGate(exponent=1)): cirq.ops.depolarize(3e-2), str(cirq.ops.CZPowGate(exponent=1)): cirq.ops.depolarize(3e-2), str(cirq.ops.CCXPowGate(exponent=1)): cirq.ops.depolarize(8e-2), str(cirq.ops.CCZPowGate(exponent=1)): cirq.ops.depolarize(8e-2), } with pytest.raises(TypeError, match="noise model varies between Pasqal's devices."): PasqalNoiseModel(cirq.devices.UNCONSTRAINED_DEVICE)
def test_default_noise(): p_qubits = cirq.NamedQubit.range(2, prefix='q') p_device = PasqalDevice(qubits=p_qubits) noise_model = PasqalNoiseModel(p_device) circuit = cirq.Circuit() Gate_l = cirq.ops.CZPowGate(exponent=2) circuit.append(Gate_l.on(p_qubits[0], p_qubits[1])) p_circuit = cirq.Circuit(circuit, device=p_device) n_mts = [] for moment in p_circuit._moments: n_mts.append(noise_model.noisy_moment(moment, p_qubits)) assert n_mts == [[ cirq.ops.CZPowGate(exponent=2).on(NamedQubit('q0'), NamedQubit('q1')), cirq.depolarize(p=0.05).on(NamedQubit('q0')), cirq.depolarize(p=0.05).on(NamedQubit('q1')), ]]
def test_default_noise(): noise_model = PasqalNoiseModel() p_qubits = ThreeDGridQubit.cube(4) p_device = PasqalDevice(control_radius=2, qubits=p_qubits) circuit = cirq.Circuit() Gate_l = cirq.ops.CZPowGate(exponent=2) circuit.append(Gate_l.on(p_qubits[0], p_qubits[1])) p_circuit = cirq.Circuit(circuit, device=p_device) n_mts = [] for moment in p_circuit._moments: n_mts.append(noise_model.noisy_moment(moment, p_qubits)) assert n_mts == [[ cirq.ops.CZPowGate(exponent=2).on(ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(0, 0, 1)), cirq.depolarize(p=0.05).on(ThreeDGridQubit(0, 0, 0)), cirq.depolarize(p=0.05).on(ThreeDGridQubit(0, 0, 1)) ]]
def test_noisy_moments(): p_qubits = cirq.NamedQubit.range(2, prefix='q') p_device = PasqalDevice(qubits=p_qubits) noise_model = PasqalNoiseModel(p_device) circuit = cirq.Circuit() circuit.append(cirq.ops.CZ(p_qubits[0], p_qubits[1])) circuit.append(cirq.ops.Z(p_qubits[1])) p_circuit = cirq.Circuit(circuit, device=p_device) n_mts = [] for moment in p_circuit._moments: n_mts.append(noise_model.noisy_moment(moment, p_qubits)) assert n_mts == [[ cirq.ops.CZ.on(NamedQubit('q0'), NamedQubit('q1')), cirq.depolarize(p=0.03).on(NamedQubit('q0')), cirq.depolarize(p=0.03).on(NamedQubit('q1')) ], [ cirq.ops.Z.on(NamedQubit('q1')), cirq.depolarize(p=0.01).on(NamedQubit('q1')) ]]
def test_noisy_moments(): noise_model = PasqalNoiseModel() p_qubits = ThreeDGridQubit.cube(4) p_device = PasqalDevice(control_radius=2, qubits=p_qubits) circuit = cirq.Circuit() circuit.append(cirq.ops.CZ(p_qubits[0], p_qubits[1])) circuit.append(cirq.ops.Z(p_qubits[1])) p_circuit = cirq.Circuit(circuit, device=p_device) n_mts = [] for moment in p_circuit._moments: n_mts.append(noise_model.noisy_moment(moment, p_qubits)) assert n_mts == [[ cirq.ops.CZ.on(ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(0, 0, 1)), cirq.depolarize(p=0.03).on(ThreeDGridQubit(0, 0, 0)), cirq.depolarize(p=0.03).on(ThreeDGridQubit(0, 0, 1)) ], [ cirq.ops.Z.on(ThreeDGridQubit(0, 0, 1)), cirq.depolarize(p=0.01).on(ThreeDGridQubit(0, 0, 1)) ]]
def test_decompose_error(): d = generic_device(3) op = (cirq.ops.CCZ**1.5).on(*(d.qubit_list())) assert d.decompose_operation(op) == [op] op = cirq.H.on(cirq.NamedQubit('q0')) decomposition = d.decompose_operation(op) assert len(decomposition) == 2 assert decomposition == [ (cirq.Y**0.5).on(cirq.NamedQubit('q0')), cirq.XPowGate(exponent=1.0, global_shift=-0.25).on(cirq.NamedQubit('q0')) ] # MeasurementGate is not a GateOperation with pytest.raises(TypeError): d.decompose_operation(cirq.ops.MeasurementGate(num_qubits=2)) # It has to be made into one assert d.is_pasqal_device_op( cirq.ops.GateOperation(cirq.ops.MeasurementGate(2), [cirq.NamedQubit('q0'), cirq.NamedQubit('q1')])) assert PasqalDevice.is_pasqal_device_op(cirq.ops.X(cirq.NamedQubit('q0')))
def test_value_equal(): dev = PasqalDevice(qubits=[cirq.NamedQubit('q1')]) assert PasqalDevice(qubits=[cirq.NamedQubit('q1')]) == dev
def test_init_errors(): line = cirq.devices.LineQubit.range(3) with pytest.raises(TypeError, match="Unsupported qubit type"): PasqalDevice(qubits=line) with pytest.raises(ValueError, match="Too many qubits"): generic_device(101)
def generic_device(num_qubits) -> PasqalDevice: return PasqalDevice(qubits=cirq.NamedQubit.range(num_qubits, prefix='q'))