コード例 #1
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_run_correlations(dtype):
    q0, q1 = cirq.LineQubit.range(2)
    simulator = TFWaveFunctionSimulator(dtype=dtype)
    circuit = cirq.Circuit.from_ops(cirq.H(q0), cirq.CNOT(q0, q1))
    for _ in range(10):
        circuit_op = simulator.simulate(circuit)
        with tf.Session() as sess:
            wf = sess.run(circuit_op)
        measurements = cirq.sample_state_vector(wf.reshape(-1), [0, 1])
コード例 #2
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_resolve_named_placeholder():
    """Resolve named placeholders via feed_dict."""
    wf = cirq.testing.random_superposition(2).astype(np.complex64)

    # placeholders can be instantiated without references in circuit gates!
    var_names = ["v_{}".format(i) for i in range(5)]
    params = np.random.randn(5)
    placeholder_circuit = cirq.Circuit()
    for i in range(5):
        placeholder_circuit += cirq.Rx(
            tf.placeholder(tf.complex64, shape=(), name=f"theta_{i}"))(q(0))

    circuit_op = TFWaveFunctionSimulator(dtype=tf.complex64).simulate(
        placeholder_circuit, initial_state=wf)
    placeholder_names = ["theta_{}:0".format(i) for i in range(5)]
    feed_dict = dict(zip(placeholder_names, params))
    with tf.Session() as sess:
        tf_result = sess.run(circuit_op, feed_dict=feed_dict).reshape(-1)

    circuit = cirq.Circuit()
    for theta in params:
        circuit += cirq.Rx(theta)(q(0))
    cirq_result = cirq.Simulator().simulate(
        circuit, initial_state=np.copy(wf)).final_state
    np.testing.assert_array_almost_equal(tf_result, cirq_result)
コード例 #3
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_run_bit_flips(dtype):
    q0, q1 = cirq.LineQubit.range(2)
    simulator = TFWaveFunctionSimulator(dtype=dtype)
    for b0 in [0, 1]:
        for b1 in [0, 1]:
            circuit = cirq.Circuit.from_ops((cirq.X**b0)(q0), (cirq.X**b1)(q1))
            circuit_op = simulator.simulate(circuit)
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            wf = sess.run(circuit_op)
        measurements = cirq.sample_state_vector(wf, [0, 1])
        np.testing.assert_array_almost_equal(measurements[0], [b0, b1])
        expected_state = np.zeros(shape=(2, 2))
        expected_state[b0][b1] = 1.0
        cirq.testing.assert_allclose_up_to_global_phase(wf.reshape(-1),
                                                        np.reshape(
                                                            expected_state, 4),
                                                        atol=1e-6)
コード例 #4
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_tf_wavefunction_simulator_vs_cirq_parametrized_two_qubit_gates(g, e):
    circuit = cirq.Circuit.from_ops(g(exponent=e)(q(0), q(1)))
    wf = cirq.testing.random_superposition(4).astype(np.complex64)
    cirq_result = cirq.Simulator().simulate(
        circuit, initial_state=np.copy(wf)).final_state
    circuit_op = TFWaveFunctionSimulator(dtype=tf.complex64).simulate(
        circuit, initial_state=np.copy(wf))
    with tf.Session() as sess:
        tf_result = sess.run(circuit_op).reshape(-1)
    np.testing.assert_array_almost_equal(tf_result, cirq_result)
コード例 #5
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_tf_wavefunction_simulator_vs_cirq_parametrized_single_qubit_gates(
        g, e):
    inst = g(e)(q(0))
    circuit = cirq.Circuit.from_ops(inst)
    # wrapped = tf_gate_wrapper(inst)
    wf = cirq.testing.random_superposition(2).astype(np.complex64)
    cirq_result = cirq.Simulator().simulate(
        circuit, initial_state=np.copy(wf)).final_state
    circuit_op = TFWaveFunctionSimulator(dtype=tf.complex64).simulate(
        circuit, initial_state=np.copy(wf))
    with tf.Session() as sess:
        tf_result = sess.run(circuit_op).reshape(-1)
    np.testing.assert_array_almost_equal(tf_result, cirq_result, decimal=4)
コード例 #6
0
def compile_tensorboard_session():
    theta = tf.Variable(np.pi)
    circuit = cirq.Circuit.from_ops(
        cirq.Ry(theta)(q(0)), cirq.CNOT(q(0), q(1)))
    tf.summary.scalar('theta', theta)

    initial_state = np.asarray([1, 0, 0, 0])
    circuit_op = TFWaveFunctionSimulator(dtype=tf.complex64).simulate(
        circuit, initial_state=initial_state)
    # with tf.Session() as sess:
    #     sess.run(tf.global_variables_initializer())
    #     wf = sess.run(circuit_op)
    # return

    tensorboard_session(circuit_op, {theta: np.pi / 2})
コード例 #7
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_tf_wavefunction_simulator_vs_cirq_single_qubit_gates(g):

    wf = np.complex64(cirq.testing.random_superposition(2))
    # print(rand_init)
    for wf in [
            np.array([1, 0], dtype=np.complex64),
            np.array([0, 1], dtype=np.complex64)
    ]:
        circuit = cirq.Circuit.from_ops(g(q(0)))
        cirq_result = cirq.Simulator().simulate(
            circuit, initial_state=np.copy(wf)).final_state

        circuit_op = TFWaveFunctionSimulator(dtype=tf.complex64).simulate(
            circuit, initial_state=np.copy(wf))
        with tf.Session() as sess:
            tf_result = sess.run(circuit_op).reshape(-1)
    # !hep-qml CHANGES: testing assertion
    np.testing.assert_array_almost_equal(tf_result, cirq_result)
コード例 #8
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_resolve_scalar_placeholder():
    """Resolve scalar placeholders via feed_dict."""
    wf = cirq.testing.random_superposition(4).astype(np.complex64)
    params = [0.83, 1.2]
    theta_x = tf.placeholder(tf.complex64, shape=(), name="theta_x")
    theta_y = tf.placeholder(tf.complex64, shape=(), name="theta_y")
    placeholder_circuit = cirq.Circuit.from_ops([
        cirq.Rx(theta_x)(q(0)),
        cirq.Ry(theta_y)(q(1)),
    ])
    circuit_op = TFWaveFunctionSimulator(dtype=tf.complex64).simulate(
        placeholder_circuit, initial_state=wf)
    feed_dict = dict(zip([theta_x, theta_y], params))
    with tf.Session() as sess:
        tf_result = sess.run(circuit_op, feed_dict=feed_dict).reshape(-1)

    circuit = cirq.Circuit.from_ops([
        cirq.Rx(params[0])(q(0)),
        cirq.Ry(params[1])(q(1)),
    ])
    cirq_result = cirq.Simulator().simulate(
        circuit, initial_state=np.copy(wf)).final_state
    np.testing.assert_array_almost_equal(tf_result, cirq_result)
コード例 #9
0
ファイル: tf_simulator_test.py プロジェクト: peterse/tf-cirq
def test_tf_wavefunction_simulator_instantiate():
    _ = TFWaveFunctionSimulator()