def test_distance(): with pytest.raises(TypeError): _ = ThreeDGridQubit(0, 0, 0).distance(cirq.GridQubit(0, 0)) for x in np.arange(-2, 3): for y in np.arange(-2, 3): for z in np.arange(-2, 3): assert ThreeDGridQubit(0, 0, 0).distance( ThreeDGridQubit(x, y, z)) == np.sqrt(x**2 + y**2 + z**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_distance(): d = cubic_device(2, 2, 1) assert d.distance(ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(1, 0, 0)) == 1 with pytest.raises(TypeError): _ = d.distance(ThreeDGridQubit(0, 0, 0), cirq.devices.LineQubit(1)) with pytest.raises(TypeError): _ = d.distance(cirq.devices.LineQubit(1), ThreeDGridQubit(0, 0, 0))
def test_to_json(): q = ThreeDGridQubit(1, 1, 1) d = q._json_dict_() assert d == { 'cirq_type': 'ThreeDGridQubit', 'row': 1, 'col': 1, 'lay': 1, }
def test_pasqal_qubit_unsupported_add(): with pytest.raises(TypeError, match='1'): _ = ThreeDGridQubit(1, 1, 1) + 1 with pytest.raises(TypeError, match='(1,)'): _ = ThreeDGridQubit(1, 1, 1) + (1, ) with pytest.raises(TypeError, match='(1, 2)'): _ = ThreeDGridQubit(1, 1, 1) + (1, 2) with pytest.raises(TypeError, match='(1, 2.0)'): _ = ThreeDGridQubit(1, 1, 1) + (1, 2.0) with pytest.raises(TypeError, match='1'): _ = ThreeDGridQubit(1, 1, 1) - 1
def test_square(): assert ThreeDGridQubit.square(2, top=1, left=1) == [ ThreeDGridQubit(1, 1, 0), ThreeDGridQubit(1, 2, 0), ThreeDGridQubit(2, 1, 0), ThreeDGridQubit(2, 2, 0) ] assert ThreeDGridQubit.square(2) == [ ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(0, 1, 0), ThreeDGridQubit(1, 0, 0), ThreeDGridQubit(1, 1, 0) ]
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_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_rec(): assert ThreeDGridQubit.rect(1, 2, top=5, left=6) == [ ThreeDGridQubit(5, 6, 0), ThreeDGridQubit(5, 7, 0) ] assert ThreeDGridQubit.rect(2, 2) == [ ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(0, 1, 0), ThreeDGridQubit(1, 0, 0), ThreeDGridQubit(1, 1, 0) ]
def test_pasqal_qubit_is_adjacent(): for ind in range(3): v = [0, 0, 0] for x in [-1, 1]: v[ind] = 1 assert ThreeDGridQubit(0, 0, 0).is_adjacent( ThreeDGridQubit(v[0], v[1], v[2])) for u in [[x, y] for y in [-1, 1]]: u.insert(ind, 0) assert not ThreeDGridQubit(0, 0, 0).is_adjacent( ThreeDGridQubit(u[0], u[1], u[2])) assert not ThreeDGridQubit(0, 0, 0).is_adjacent(ThreeDGridQubit(2, 0, 0)) assert (ThreeDGridQubit(500, 999, 1500).is_adjacent(ThreeDGridQubit(501, 999, 1500))) assert not (ThreeDGridQubit(500, 999, 1500).is_adjacent( ThreeDGridQubit(5034, 999, 1500)))
def test_pasqal_qubit_ordering(): for i in range(8): v = [int(x) for x in bin(i)[2:].zfill(3)] assert ThreeDGridQubit(0, 0, 0) <= ThreeDGridQubit(v[0], v[1], v[2]) assert ThreeDGridQubit(1, 1, 1) >= ThreeDGridQubit(v[0], v[1], v[2]) if i >= 1: assert ThreeDGridQubit(0, 0, 0) < ThreeDGridQubit(v[0], v[1], v[2]) if i < 7: assert ThreeDGridQubit(1, 1, 1) > ThreeDGridQubit(v[0], v[1], v[2])
def test_get_op_string(): noise_model = PasqalNoiseModel() p_qubits = ThreeDGridQubit.cube(4) circuit = cirq.Circuit() circuit.append(cirq.ops.H(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:'): _ = cirq.pasqal.pasqal_noise_model.get_op_string(circuit)
def test_validate_operation_errors(): d = cubic_device(3, 3, 3) qlist = d.qubit_list() too_many_qubits_op = cirq.ops.X.controlled(len(qlist) - 1) too_many_qubits_op = cirq.ops.GateOperation(too_many_qubits_op, qlist) with pytest.raises(ValueError, match="Too many qubits acted on in parallel by"): d.validate_operation(too_many_qubits_op) with pytest.raises(ValueError, match="Unsupported operation"): d.validate_operation(ThreeDGridQubit(0, 0, 0)) with pytest.raises(ValueError, match="cirq.H is not a supported gate"): d.validate_operation(cirq.ops.H.on(ThreeDGridQubit(0, 0, 0))) with pytest.raises(ValueError, match="is not a 3D grid qubit for gate cirq.X"): d.validate_operation(cirq.X.on(cirq.LineQubit(0))) with pytest.raises(ValueError, match="are too far away"): d.validate_operation( cirq.CZ.on(ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(3, 3, 3))) with pytest.raises(ValueError, match="Too many Z gates in parallel"): d.validate_operation(cirq.ParallelGateOperation(cirq.ops.Z, d.qubits)) with pytest.raises(ValueError, match="Bad number of X/Y gates in parallel"): d.validate_operation( cirq.ParallelGateOperation(cirq.ops.X, d.qubit_list()[1:])) assert d.validate_operation( cirq.ops.GateOperation(cirq.ops.MeasurementGate(1), [ThreeDGridQubit(0, 0, 0)])) is None
def test_init(): d = cubic_device(2, 2, 2, holes=[ThreeDGridQubit(1, 1, 1)]) us = cirq.value.Duration(micros=1) ms = cirq.value.Duration(micros=10**3) q000 = ThreeDGridQubit(0, 0, 0) q001 = ThreeDGridQubit(0, 0, 1) q010 = ThreeDGridQubit(0, 1, 0) q011 = ThreeDGridQubit(0, 1, 1) q100 = ThreeDGridQubit(1, 0, 0) q101 = ThreeDGridQubit(1, 0, 1) q110 = ThreeDGridQubit(1, 1, 0) assert d.qubit_set() == {q000, q001, q010, q011, q100, q101, q110} assert d.qubit_list() == [q000, q001, q010, q011, q100, q101, q110] assert d.duration_of( cirq.ops.GateOperation(cirq.ops.IdentityGate(1), [q000])) == 2 * us assert d.duration_of(cirq.ops.measure(q000)) == 5 * ms with pytest.raises(ValueError): _ = d.duration_of(cirq.ops.SingleQubitGate().on(q000))
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 = 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_repr(): assert repr(ThreeDGridQubit(4, -25, 109)) == 'pasqal.ThreeDGridQubit(4, -25, 109)'
def test_str(): assert str(ThreeDGridQubit(4, -25, 109)) == '(4, -25, 109)'
def test_grid_qubit_eq(): eq = cirq.testing.EqualsTester() eq.make_equality_group(lambda: ThreeDGridQubit(0, 0, 0)) eq.make_equality_group(lambda: ThreeDGridQubit(1, 0, 0)) eq.make_equality_group(lambda: ThreeDGridQubit(0, 1, 0)) eq.make_equality_group(lambda: ThreeDGridQubit(50, 25, 25))
def test_pasqal_qubit_add_subtract(): assert ThreeDGridQubit(1, 2, 3) + (2, 5, 7) == ThreeDGridQubit(3, 7, 10) assert ThreeDGridQubit(1, 2, 3) + (0, 0, 0) == ThreeDGridQubit(1, 2, 3) assert ThreeDGridQubit(1, 2, 3) + (-1, 0, 0) == ThreeDGridQubit(0, 2, 3) assert ThreeDGridQubit(1, 2, 3) - (2, 5, 7) == ThreeDGridQubit(-1, -3, -4) assert ThreeDGridQubit(1, 2, 3) - (0, 0, 0) == ThreeDGridQubit(1, 2, 3) assert ThreeDGridQubit(1, 2, 3) - (-1, 0, 0) == ThreeDGridQubit(2, 2, 3) assert (2, 5, 7) + ThreeDGridQubit(1, 2, 3) == ThreeDGridQubit(3, 7, 10) assert (2, 5, 7) - ThreeDGridQubit(1, 2, 3) == ThreeDGridQubit(1, 3, 4)
def test_qubit_set(): assert cubic_device(2, 2, 2).qubit_set() == set(ThreeDGridQubit.cube(2, 0, 0, 0))
def test_parrallelep(): assert ThreeDGridQubit.parallelep(1, 2, 2, top=5, left=6, upper=7) == [ ThreeDGridQubit(5, 6, 7), ThreeDGridQubit(5, 6, 8), ThreeDGridQubit(5, 7, 7), ThreeDGridQubit(5, 7, 8), ] assert ThreeDGridQubit.parallelep(2, 2, 2) == [ ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(0, 0, 1), ThreeDGridQubit(0, 1, 0), ThreeDGridQubit(0, 1, 1), ThreeDGridQubit(1, 0, 0), ThreeDGridQubit(1, 0, 1), ThreeDGridQubit(1, 1, 0), ThreeDGridQubit(1, 1, 1) ]
def test_cube(): assert ThreeDGridQubit.cube(2, top=1, left=1, upper=1) == [ ThreeDGridQubit(1, 1, 1), ThreeDGridQubit(1, 1, 2), ThreeDGridQubit(1, 2, 1), ThreeDGridQubit(1, 2, 2), ThreeDGridQubit(2, 1, 1), ThreeDGridQubit(2, 1, 2), ThreeDGridQubit(2, 2, 1), ThreeDGridQubit(2, 2, 2) ] assert ThreeDGridQubit.cube(2) == [ ThreeDGridQubit(0, 0, 0), ThreeDGridQubit(0, 0, 1), ThreeDGridQubit(0, 1, 0), ThreeDGridQubit(0, 1, 1), ThreeDGridQubit(1, 0, 0), ThreeDGridQubit(1, 0, 1), ThreeDGridQubit(1, 1, 0), ThreeDGridQubit(1, 1, 1), ]
def test_pasqal_qubit_neg(): assert -ThreeDGridQubit(1, 2, 3) == ThreeDGridQubit(-1, -2, -3)
def test_pasqal_qubit_init(): q = ThreeDGridQubit(3, 4, 5) assert q.row == 3 assert q.col == 4 assert q.lay == 5
def test_pasqal_qubit_neighbors(): expected = { ThreeDGridQubit(1, 1, 2), ThreeDGridQubit(1, 2, 1), ThreeDGridQubit(2, 1, 1), ThreeDGridQubit(0, 1, 1), ThreeDGridQubit(1, 0, 1), ThreeDGridQubit(1, 1, 0) } assert ThreeDGridQubit(1, 1, 1).neighbors() == expected # Restrict to a list of qubits restricted_qubits = [ThreeDGridQubit(2, 1, 1), ThreeDGridQubit(2, 2, 1)] expected2 = {ThreeDGridQubit(2, 1, 1)} assert ThreeDGridQubit(1, 1, 1).neighbors(restricted_qubits) == expected2
def test_value_equal(): dev = cirq.pasqal.PasqalDevice(control_radius=5, qubits=[ThreeDGridQubit(1, 1, 1)]) assert cirq.pasqal.PasqalDevice(control_radius=5, qubits=[ThreeDGridQubit(1, 1, 1)]) == dev
def test_comparison_key(): assert ThreeDGridQubit(3, 4, 5)._comparison_key() == (3, 4, 5)