コード例 #1
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def test_run_parameters_not_resolved():
    a = cirq.LineQubit(0)
    simulator = cirq.MPSSimulator()
    circuit = cirq.Circuit(
        cirq.XPowGate(exponent=sympy.Symbol('a'))(a), cirq.measure(a))
    with pytest.raises(ValueError, match='symbols were not specified'):
        _ = simulator.run_sweep(circuit, cirq.ParamResolver({}))
コード例 #2
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def test_sample_seed():
    q = cirq.NamedQubit('q')
    circuit = cirq.Circuit(cirq.H(q), cirq.measure(q))
    simulator = cirq.MPSSimulator(seed=1234)
    result = simulator.run(circuit, repetitions=20)
    measured = result.measurements['q']
    result_string = ''.join(map(lambda x: str(int(x[0])), measured))
    assert result_string == '01011001110111011011'
コード例 #3
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def test_measurement_str():
    q0 = cirq.NamedQid('q0', dimension=3)
    circuit = cirq.Circuit(cirq.measure(q0))

    simulator = cirq.MPSSimulator()
    result = simulator.run(circuit, repetitions=7)

    assert str(result) == "q0 (d=3)=0000000"
コード例 #4
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def test_measurement():
    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit(cirq.X(q0), cirq.H(q1), cirq.measure(q1))

    simulator = cirq.MPSSimulator()

    result = simulator.run(circuit, repetitions=100)
    assert sum(result.measurements['1'])[0] < 80
    assert sum(result.measurements['1'])[0] > 20
コード例 #5
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def test_state_copy():
    sim = cirq.MPSSimulator()

    q = cirq.LineQubit(0)
    circuit = cirq.Circuit(cirq.H(q), cirq.H(q))

    state_Ms = []
    for step in sim.simulate_moment_steps(circuit):
        state_Ms.append(step.state.M)
    for x, y in itertools.combinations(state_Ms, 2):
        assert len(x) == len(y)
        for i in range(len(x)):
            assert not np.shares_memory(x[i], y[i])
コード例 #6
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def assert_same_output_as_dense(circuit, qubit_order, initial_state=0):
    mps_simulator = cirq.MPSSimulator()
    ref_simulator = cirq.Simulator()

    actual = mps_simulator.simulate(circuit,
                                    qubit_order=qubit_order,
                                    initial_state=initial_state)
    expected = ref_simulator.simulate(circuit,
                                      qubit_order=qubit_order,
                                      initial_state=initial_state)
    np.testing.assert_almost_equal(actual.final_state.to_numpy(),
                                   expected.final_state_vector)
    assert len(actual.measurements) == 0
コード例 #7
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def test_simulate_moment_steps_sample():
    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1))

    simulator = cirq.MPSSimulator()

    for i, step in enumerate(simulator.simulate_moment_steps(circuit)):
        if i == 0:
            np.testing.assert_almost_equal(
                step._simulator_state().to_numpy(),
                np.asarray([1.0 / math.sqrt(2), 0.0, 1.0 / math.sqrt(2), 0.0]),
            )
            assert str(
                step
            ) == "[array([[[0.70710678+0.j, 0.70710678+0.j]]]), array([[[1., 0.]]])]"
            samples = step.sample([q0, q1], repetitions=10)
            for sample in samples:
                assert np.array_equal(sample, [True, False]) or np.array_equal(
                    sample, [False, False])
            np.testing.assert_almost_equal(
                step._simulator_state().to_numpy(),
                np.asarray([1.0 / math.sqrt(2), 0.0, 1.0 / math.sqrt(2), 0.0]),
            )
        else:
            np.testing.assert_almost_equal(
                step._simulator_state().to_numpy(),
                np.asarray([1.0 / math.sqrt(2), 0.0, 0.0, 1.0 / math.sqrt(2)]),
            )
            assert (str(step) == """[array([[[0.84089642+0.j, 0.        +0.j],
        [0.        +0.j, 0.84089642+0.j]]]), array([[[0.84089642+0.j, 0.        +0.j]],

       [[0.        +0.j, 0.84089642+0.j]]])]""")
            samples = step.sample([q0, q1], repetitions=10)
            for sample in samples:
                assert np.array_equal(sample, [True, True]) or np.array_equal(
                    sample, [False, False])
コード例 #8
0
ファイル: mps_simulator_test.py プロジェクト: snsunx/Cirq
def test_run_no_repetitions():
    q0 = cirq.LineQubit(0)
    simulator = cirq.MPSSimulator()
    circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0))
    result = simulator.run(circuit, repetitions=0)
    assert len(result.measurements['0']) == 0