def bit_flip_circuit(flip0, flip1): q1, q2 = XmonQubit(0, 0), XmonQubit(0, 1) g1, g2 = ExpWGate(half_turns=flip0)(q1), ExpWGate(half_turns=flip1)(q2) m1, m2 = XmonMeasurementGate('q1')(q1), XmonMeasurementGate('q2')(q2) circuit = Circuit() circuit.append([g1, g2, m1, m2]) return circuit
def test_measurement_keys_repeat(scheduler): circuit = Circuit() meas = XmonMeasurementGate('a') circuit.append([meas.on(Q1), X.on(Q1), X.on(Q2), meas.on(Q2)]) simulator = xmon_simulator.XmonSimulator() with pytest.raises(ValueError, message='Repeated Measurement key a'): run(simulator, circuit, scheduler)
def test_measurement_qubit_order(scheduler): circuit = Circuit() meas = XmonMeasurementGate(key='') circuit.append(X(Q2)) circuit.append(X(Q1)) circuit.append([meas.on(Q1, Q3, Q2)]) simulator = xmon_simulator.XmonSimulator() result = run(simulator, circuit, scheduler) np.testing.assert_equal(result.measurements[''], [[True, False, True]])
def test_inverted_measurement_multiple_qubits(scheduler): circuit = Circuit.from_ops( XmonMeasurementGate('a', invert_mask=(False, True))(Q1, Q2), XmonMeasurementGate('b', invert_mask=(True, False))(Q1, Q2), XmonMeasurementGate('c', invert_mask=(True, False))(Q2, Q1)) simulator = xmon_simulator.XmonSimulator() result = run(simulator, circuit, scheduler) np.testing.assert_equal(result.measurements['a'], [[False, True]]) np.testing.assert_equal(result.measurements['b'], [[True, False]]) np.testing.assert_equal(result.measurements['c'], [[True, False]])
def test_run(scheduler): np.random.seed(0) circuit = basic_circuit() circuit.append( [XmonMeasurementGate(key='a')(Q1), XmonMeasurementGate(key='b')(Q2)]) simulator = xmon_simulator.XmonSimulator() result = run(simulator, circuit, scheduler) assert result.measurements == {'a': [False], 'b': [False]}
def test_validate_circuit_repeat_measurement_keys(): d = square_device(3, 3) circuit = Circuit() circuit.append([ XmonMeasurementGate('a').on(XmonQubit(0, 0)), XmonMeasurementGate('a').on(XmonQubit(0, 1)) ]) with pytest.raises(ValueError, message='Measurement key a repeated'): d.validate_circuit(circuit)
def test_run_no_sharing_few_qubits(scheduler): np.random.seed(0) circuit = basic_circuit() circuit.append( [XmonMeasurementGate(key='a')(Q1), XmonMeasurementGate(key='b')(Q2)]) simulator = xmon_simulator.XmonSimulator( xmon_simulator.XmonOptions(min_qubits_before_shard=0)) result = run(simulator, circuit, scheduler) np.testing.assert_equal(result.measurements['a'], [[False]]) np.testing.assert_equal(result.measurements['b'], [[False]])
def test_validate_schedule_repeat_measurement_keys(): d = square_device(3, 3) s = Schedule(d, [ ScheduledOperation.op_at_on( XmonMeasurementGate('a').on(XmonQubit(0, 0)), Timestamp(), d), ScheduledOperation.op_at_on( XmonMeasurementGate('a').on(XmonQubit(0, 1)), Timestamp(), d), ]) with pytest.raises(ValueError, message='Measurement key a repeated'): d.validate_schedule(s)
def test_inverted_measurement(scheduler): circuit = Circuit.from_ops( XmonMeasurementGate('a', invert_mask=(False, ))(Q1), X(Q1), XmonMeasurementGate('b', invert_mask=(False, ))(Q1), XmonMeasurementGate('c', invert_mask=(True, ))(Q1), X(Q1), XmonMeasurementGate('d', invert_mask=(True, ))(Q1)) simulator = xmon_simulator.XmonSimulator() result = run(simulator, circuit, scheduler) assert { 'a': [[False]], 'b': [[True]], 'c': [[False]], 'd': [[True]] } == result.measurements
def test_simulate_moment_steps(): np.random.seed(0) circuit = basic_circuit() circuit.append( [XmonMeasurementGate(key='a')(Q1), XmonMeasurementGate(key='b')(Q2)]) simulator = xmon_simulator.XmonSimulator() results = [] for step in simulator.simulate_moment_steps(circuit): results.append(step) expected = [{}, {}, {}, {}, {'a': [False], 'b': [False]}] assert len(results) == len(expected) assert all(a.measurements == b for a, b in zip(results, expected))
def test_composite_gates(scheduler): circuit = Circuit() circuit.append([X(Q1), CNOT(Q1, Q2)]) m = XmonMeasurementGate('a') circuit.append([m(Q1, Q2)]) simulator = xmon_simulator.XmonSimulator() result = run(simulator, circuit, scheduler) np.testing.assert_equal(result.measurements['a'], [[True, True]])
def test_handedness_of_xmon_gates(): circuit = Circuit.from_ops( ExpWGate(half_turns=-0.5).on(Q1), ExpZGate(half_turns=-0.5).on(Q1), ExpWGate(axis_half_turns=0.5, half_turns=0.5).on(Q1), XmonMeasurementGate(key='').on(Q1), ) result = xmon_simulator.XmonSimulator().run(circuit) np.testing.assert_equal(result.measurements[''], [[True]])
def test_handedness_of_basic_gates(): circuit = Circuit.from_ops( X(Q1)**-0.5, Z(Q1)**-0.5, Y(Q1)**0.5, XmonMeasurementGate(key='').on(Q1), ) result = xmon_simulator.XmonSimulator().run(circuit) np.testing.assert_equal(result.measurements[''], [[True]])
def large_circuit(): np.random.seed(0) qubits = [XmonQubit(i, 0) for i in range(10)] sqrt_x = ExpWGate(half_turns=0.5, axis_half_turns=0.0) cz = Exp11Gate() circuit = Circuit() for _ in range(11): circuit.append( [sqrt_x(qubit) for qubit in qubits if np.random.random() < 0.5]) circuit.append([cz(qubits[i], qubits[i + 1]) for i in range(9)]) circuit.append([XmonMeasurementGate(key='meas')(*qubits)]) return circuit
def test_run_circuit_sweep(): circuit = Circuit.from_ops( ExpWGate(half_turns=Symbol('a')).on(Q1), XmonMeasurementGate('m').on(Q1), ) sweep = Linspace('a', 0, 10, 11) simulator = xmon_simulator.XmonSimulator() for i, result in enumerate( simulator.run_sweep(circuit, sweep, repetitions=1)): assert result.params['a'] == i assert result.measurements['m'] == [i % 2 != 0]
def test_measurement_multiple_measurements_qubit_order(scheduler): circuit = Circuit() measure_a = XmonMeasurementGate('a') measure_b = XmonMeasurementGate('b') circuit.append(X(Q1)) circuit.append([measure_a.on(Q1, Q2)]) circuit.append([measure_b.on(Q2, Q1)]) simulator = xmon_simulator.XmonSimulator() result = run(simulator, circuit, scheduler) np.testing.assert_equal(result.measurements['a'], [[True, False]]) np.testing.assert_equal(result.measurements['b'], [[False, True]])
def circuit_init(meas=True): if meas: yield XmonMeasurementGate(key='q0')(q[0]) yield XmonMeasurementGate(key='q1')(q[1]) yield XmonMeasurementGate(key='q2')(q[2]) yield XmonMeasurementGate(key='q3')(q[3])
def circuit_init(meas=True): if meas: yield XmonMeasurementGate(key='q0')(q[0])
def circuit_init(meas=True): if meas: yield XmonMeasurementGate(key='q')(target_q)
def circuit_init_again(meas=True): if meas: yield XmonMeasurementGate(key='qubit')(final)
def deutsch_circuit_init(meas=True): yield cirq.H.on(q1) if meas: yield XmonMeasurementGate(key='q1')(q1)
def test_validate_measurement_non_adjacent_qubits_ok(): d = square_device(3, 3) d.validate_operation( ops.Operation(XmonMeasurementGate(key=''), (XmonQubit(0, 0), XmonQubit(2, 0))))