Example #1
1
def test_simulate_state_different_order_of_qubits(scheduler):
    simulator = cg.XmonSimulator()
    result = simulate(simulator,
                      basic_circuit(),
                      scheduler,
                      qubit_order=[Q2, Q1])
    np.testing.assert_almost_equal(result.final_state,
                                   np.array([0.5j, -0.5, 0.5, -0.5j]))
Example #2
1
def test_param_resolver_param_dict():
    exp_w = cg.ExpWGate(half_turns=cirq.Symbol('a'), axis_half_turns=0.0)
    circuit = cirq.Circuit()
    circuit.append(exp_w(Q1))
    resolver = cirq.ParamResolver({'a': 0.5})

    simulator = cg.XmonSimulator()
    result = simulator.run(circuit, resolver)
    assert result.params.param_dict == {'a': 0.5}
Example #3
1
def test_measurement_keys_repeat(scheduler):
    circuit = cirq.Circuit(device=test_device)
    circuit.append([
        cirq.measure(Q1, key='a'),
        cirq.X.on(Q1),
        cirq.X.on(Q2),
        cirq.measure(Q2, key='a')
    ])
    simulator = cg.XmonSimulator()
    with pytest.raises(ValueError, message='Repeated Measurement key a'):
        run(simulator, circuit, scheduler)
Example #4
0
def test_run_initial_state_ndarray_not_upconvertable(scheduler):
    simulator = cg.XmonSimulator()

    with pytest.raises(TypeError):
        _ = run(simulator,
                basic_circuit(),
                scheduler,
                initial_state=np.array([0, 0, 1, 0], dtype=np.float128))
Example #5
0
def test_simulate_initial_state_ndarray_upconvert(scheduler):
    simulator = cg.XmonSimulator()
    result = simulate(simulator,
                      basic_circuit(),
                      scheduler,
                      initial_state=np.array([0, 0, 1, 0], dtype=np.float32))
    np.testing.assert_almost_equal(result.final_state,
                                   np.array([0.5, 0.5j, 0.5j, 0.5]))
Example #6
0
def test_handedness_of_z_gate():
    circuit = cirq.Circuit.from_ops(cirq.H(Q1), cirq.Z(Q1)**0.5)
    simulator = cg.XmonSimulator()
    result = list(simulator.simulate_moment_steps(circuit))[-1]
    cirq.testing.assert_allclose_up_to_global_phase(result.state_vector(),
                                                    np.array([1, 1j]) *
                                                    np.sqrt(0.5),
                                                    atol=1e-7)
Example #7
0
def test_measurement_qubit_order(scheduler):
    circuit = cirq.Circuit()
    circuit.append(cirq.X(Q2))
    circuit.append(cirq.X(Q1))
    circuit.append([cirq.measure(Q1, Q3, Q2, key='')])
    simulator = cg.XmonSimulator()
    result = run(simulator, circuit, scheduler)
    np.testing.assert_equal(result.measurements[''], [[True, False, True]])
Example #8
0
def test_unsupported_gate_composite(scheduler):
    circuit = cirq.Circuit()
    gate = UnsupportedGate()
    circuit.append([cirq.H(Q1), gate(Q2)])

    simulator = cg.XmonSimulator()
    with pytest.raises(TypeError, message="UnsupportedGate"):
        _ = run(simulator, circuit, scheduler)
Example #9
0
def test_composite_gates(scheduler):
    circuit = cirq.Circuit()
    circuit.append([cirq.X(Q1), cirq.CNOT(Q1, Q2)])
    circuit.append([cirq.measure(Q1, Q2, key='a')])

    simulator = cg.XmonSimulator()
    result = run(simulator, circuit, scheduler)
    np.testing.assert_equal(result.measurements['a'], [[True, True]])
Example #10
0
def test_consistent_seeded_run_sharded(scheduler):
    circuit = large_circuit()

    simulator = cg.XmonSimulator()
    result = run(simulator, circuit, scheduler)
    np.testing.assert_equal(
        result.measurements['meas'],
        [[False, False, True, True, True, True, False, True, False, False]])
Example #11
0
def test_handedness_of_xmon_exp_x_gate():
    circuit = cirq.Circuit.from_ops(cg.ExpWGate(half_turns=0.5).on(Q1))
    simulator = cg.XmonSimulator()
    result = list(simulator.simulate_moment_steps(circuit))[-1]
    cirq.testing.assert_allclose_up_to_global_phase(result.state(),
                                                    np.array([1, -1j]) *
                                                    np.sqrt(0.5),
                                                    atol=1e-7)
Example #12
0
def test_handedness_of_basic_gates():
    circuit = cirq.Circuit.from_ops(
        cirq.X(Q1)**-0.5,
        cirq.Z(Q1)**-0.5,
        cirq.Y(Q1)**0.5,
        cg.XmonMeasurementGate(key='').on(Q1),
    )
    result = cg.XmonSimulator().run(circuit)
    np.testing.assert_equal(result.measurements[''], [[True]])
Example #13
0
def test_param_resolver_param_dict():
    exp_w = cirq.PhasedXPowGate(exponent=cirq.Symbol('a'), phase_exponent=0.0)
    circuit = cirq.Circuit()
    circuit.append(exp_w(Q1))
    resolver = cirq.ParamResolver({'a': 0.5})

    simulator = cg.XmonSimulator()
    result = simulator.run(circuit, resolver)
    assert result.params.param_dict == {'a': 0.5}
Example #14
0
def compute_gate(circuit, resolver, num_qubits=1):
    simulator = cg.XmonSimulator()
    gate = []
    for initial_state in range(1 << num_qubits):
        result = simulator.simulate(circuit,
                                    initial_state=initial_state,
                                    param_resolver=resolver)
        gate.append(result.final_state)
    return np.array(gate).transpose()
Example #15
0
def test_circuit_repetitions():
    sim = cg.XmonSimulator()
    circuit = bit_flip_circuit(1, 1)

    result = sim.run(circuit, repetitions=10)
    assert result.params.param_dict == {}
    assert result.repetitions == 10
    np.testing.assert_equal(result.measurements['q1'], [[True]] * 10)
    np.testing.assert_equal(result.measurements['q2'], [[True]] * 10)
Example #16
0
def test_handedness_of_xmon_gates():
    circuit = cirq.Circuit.from_ops(
        cg.ExpWGate(exponent=-0.5).on(Q1),
        cirq.Z(Q1)**-0.5,
        cg.ExpWGate(phase_exponent=0.5, exponent=0.5).on(Q1),
        cirq.MeasurementGate(key='').on(Q1),
    )
    result = cg.XmonSimulator().run(circuit)
    np.testing.assert_equal(result.measurements[''], [[True]])
Example #17
0
def test_measurement_multiple_measurements_qubit_order(scheduler):
    circuit = cirq.Circuit(device=test_device)
    circuit.append(cirq.X(Q1))
    circuit.append([cirq.measure(Q1, Q2, key='a')])
    circuit.append([cirq.measure(Q2, Q1, key='b')])
    simulator = cg.XmonSimulator()
    result = run(simulator, circuit, scheduler)
    np.testing.assert_equal(result.measurements['a'], [[True, False]])
    np.testing.assert_equal(result.measurements['b'], [[False, True]])
Example #18
0
def test_handedness_of_xmon_gates():
    circuit = cirq.Circuit.from_ops(
        cg.ExpWGate(half_turns=-0.5).on(Q1),
        cg.ExpZGate(half_turns=-0.5).on(Q1),
        cg.ExpWGate(axis_half_turns=0.5, half_turns=0.5).on(Q1),
        cg.XmonMeasurementGate(key='').on(Q1),
    )
    result = cg.XmonSimulator().run(circuit)
    np.testing.assert_equal(result.measurements[''], [[True]])
Example #19
0
def test_handedness_of_xmon_exp_11_gate():
    circuit = cirq.Circuit.from_ops(cirq.H(Q1), cirq.H(Q2),
                                    cg.Exp11Gate(half_turns=0.5).on(Q1, Q2))
    simulator = cg.XmonSimulator()
    result = list(simulator.simulate_moment_steps(circuit))[-1]
    print(np.round(result.state(), 3))
    cirq.testing.assert_allclose_up_to_global_phase(result.state(),
                                                    np.array([1, 1, 1, 1j]) /
                                                    2,
                                                    atol=1e-7)
Example #20
0
def test_inverted_measurement_multiple_qubits(scheduler):
    circuit = cirq.Circuit.from_ops(
        cg.XmonMeasurementGate('a', invert_mask=(False, True))(Q1, Q2),
        cg.XmonMeasurementGate('b', invert_mask=(True, False))(Q1, Q2),
        cg.XmonMeasurementGate('c', invert_mask=(True, False))(Q2, Q1))
    simulator = cg.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]])
Example #21
0
def test_param_resolver_param_dict():
    exp_w = cirq.PhasedXPowGate(exponent=sympy.Symbol('a'), phase_exponent=0.0)
    circuit = cirq.Circuit(device=test_device)
    circuit.append(exp_w(Q1))
    circuit.append([cirq.measure(Q1, key='meas')])
    resolver = cirq.ParamResolver({'a': 0.5})

    simulator = cg.XmonSimulator()
    result = simulator.run(circuit, resolver)
    assert result.params.param_dict == {'a': 0.5}
Example #22
0
def test_handedness_of_xmon_gates():
    circuit = cirq.Circuit.from_ops(
        cirq.X(Q1)**-0.5,
        cirq.Z(Q1)**-0.5,
        cirq.Y(Q1)**0.5,
        cirq.measure(Q1, key=''),
        device=test_device,
    )
    result = cg.XmonSimulator().run(circuit)
    np.testing.assert_equal(result.measurements[''], [[True]])
Example #23
0
def test_simulate_moment_steps_set_state():
    np.random.seed(0)
    circuit = basic_circuit()

    simulator = cg.XmonSimulator()
    step = simulator.simulate_moment_steps(circuit)

    result = next(step)
    result.set_state(0)
    np.testing.assert_almost_equal(result.state(), np.array([1, 0, 0, 0]))
Example #24
0
def test_measurement_multiple_measurements_qubit_order(scheduler):
    circuit = cirq.Circuit()
    measure_a = cg.XmonMeasurementGate('a')
    measure_b = cg.XmonMeasurementGate('b')
    circuit.append(cirq.X(Q1))
    circuit.append([measure_a.on(Q1, Q2)])
    circuit.append([measure_b.on(Q2, Q1)])
    simulator = cg.XmonSimulator()
    result = run(simulator, circuit, scheduler)
    np.testing.assert_equal(result.measurements['a'], [[True, False]])
    np.testing.assert_equal(result.measurements['b'], [[False, True]])
Example #25
0
def test_run_no_sharing_few_qubits(scheduler):
    np.random.seed(0)
    circuit = basic_circuit()
    circuit.append([cirq.measure(Q1, key='a'),
                    cirq.measure(Q2, key='b')],
                   strategy=InsertStrategy.NEW_THEN_INLINE)

    simulator = cg.XmonSimulator(cg.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'], [[True]])
Example #26
0
def test_handedness_of_xmon_exp_11_gate():
    circuit = cirq.Circuit.from_ops(cirq.H(Q1),
                                    cirq.H(Q2),
                                    cirq.CZ(Q1, Q2)**0.5,
                                    device=test_device)
    simulator = cg.XmonSimulator()
    result = list(simulator.simulate_moment_steps(circuit))[-1]
    cirq.testing.assert_allclose_up_to_global_phase(result.state_vector(),
                                                    np.array([1, 1, 1, 1j]) /
                                                    2,
                                                    atol=1e-7)
Example #27
0
def test_simulate_moment_steps_set_state_2():
    np.random.seed(0)
    circuit = basic_circuit()

    simulator = cg.XmonSimulator()
    step = simulator.simulate_moment_steps(circuit)

    result = next(step)
    result.set_state_vector(np.array([1j, 0, 0, 0], dtype=np.complex64))
    np.testing.assert_almost_equal(result.state_vector(),
                                   np.array([1j, 0, 0, 0], dtype=np.complex64))
Example #28
0
def test_circuit_repetitions_optimized_regression():
    sim = cg.XmonSimulator()
    circuit = bit_flip_circuit(1, 1)

    # When not optimized this takes around 20 seconds to run, otherwise it
    # runs in less than a second.
    start = time.time()
    result = sim.run(circuit, repetitions=10000)
    assert result.repetitions == 10000
    end = time.time()
    assert end - start < 1.0
Example #29
0
def test_run_no_sharing_few_qubits(scheduler):
    np.random.seed(0)
    circuit = basic_circuit()
    circuit.append(
        [cirq.MeasurementGate(key='a')(Q1),
         cirq.MeasurementGate(key='b')(Q2)])

    simulator = cg.XmonSimulator(cg.XmonOptions(min_qubits_before_shard=0))
    result = run(simulator, circuit, scheduler)
    np.testing.assert_equal(result.measurements['a'], [[True]])
    np.testing.assert_equal(result.measurements['b'], [[False]])
Example #30
0
def test_simulate_moment_steps_state():
    np.random.seed(0)
    circuit = basic_circuit()

    simulator = cg.XmonSimulator()
    results = []
    for step in simulator.simulate_moment_steps(circuit):
        results.append(step.state())
    np.testing.assert_almost_equal(
        results,
        np.array([[0.5, 0.5j, 0.5j, -0.5], [0.5, 0.5j, 0.5j, 0.5],
                  [-0.5, 0.5j, 0.5j, -0.5], [0.5j, 0.5, -0.5, -0.5j]]))