コード例 #1
0
def test_wavefunction_partial_trace_as_mixture_mixed_result():
    state = np.array([1, 0, 0, 1]) / np.sqrt(2)
    truth = ((0.5, np.array([1, 0])), (0.5, np.array([0, 1])))
    for q1 in [0, 1]:
        mixture = cirq.wavefunction_partial_trace_as_mixture(state, [q1],
                                                             atol=1e-8)
        assert mixtures_equal(mixture, truth)

    state = np.array([0, 1, 1, 0, 1, 0, 0, 0]).reshape((2, 2, 2)) / np.sqrt(3)
    truth = ((1 / 3, np.array([0.0, 1.0])), (2 / 3, np.array([1.0, 0.0])))
    for q1 in [0, 1, 2]:
        mixture = cirq.wavefunction_partial_trace_as_mixture(state, [q1],
                                                             atol=1e-8)
        assert mixtures_equal(mixture, truth)

    state = np.array([1, 0, 0, 0, 0, 0, 0, 1]).reshape((2, 2, 2)) / np.sqrt(2)
    truth = ((0.5, np.array([1, 0])), (0.5, np.array([0, 1])))
    for q1 in [0, 1, 2]:
        mixture = cirq.wavefunction_partial_trace_as_mixture(state, [q1],
                                                             atol=1e-8)
        assert mixtures_equal(mixture, truth)

    truth = ((0.5, np.array([1, 0, 0, 0]).reshape(
        (2, 2))), (0.5, np.array([0, 0, 0, 1]).reshape((2, 2))))
    for (q1, q2) in [(0, 1), (0, 2), (1, 2)]:
        mixture = cirq.wavefunction_partial_trace_as_mixture(state, [q1, q2],
                                                             atol=1e-8)
        assert mixtures_equal(mixture, truth)
コード例 #2
0
def test_deprecated():
    a = np.arange(4) / np.linalg.norm(np.arange(4))
    with cirq.testing.assert_logs('subwavefunction', 'sub_state_vector',
                                  'deprecated'):
        _ = cirq.subwavefunction(a, [0, 1], atol=1e-8)

    with cirq.testing.assert_logs('wavefunction', 'state_vector',
                                  'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.sub_state_vector(wavefunction=a,
                                  keep_indices=[0, 1],
                                  atol=1e-8)

    with cirq.testing.assert_logs(
            'wavefunction_partial_trace_as_mixture',
            'partial_trace_of_state_vector_as_mixture',
            'deprecated',
    ):
        _ = cirq.wavefunction_partial_trace_as_mixture(a, [0])

    with cirq.testing.assert_logs('wavefunction', 'state_vector',
                                  'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.partial_trace_of_state_vector_as_mixture(wavefunction=a,
                                                          keep_indices=[0])
コード例 #3
0
print('symerr (should be zero):', np.linalg.norm(H - H.conj().T))

lam, V = np.linalg.eigh(H)
# ground state wavefunction
psi = V[:, 0] / np.linalg.norm(V[:, 0])

trained_disc_weights = tf.Variable(np.array([
    -5.902424, 5.235119, 2.9735384, -4.027759, -0.45231304, -10.262014,
    2.189722, 6.306804, 1.9912083, -13.428224, -9.827148, 0.3823985,
    -3.0864358, -9.370758, 8.842436, -8.806886, 7.2321877, 7.3172007,
    6.5709624, -15.352012, -2.5790832, 3.435183, 7.1098614, 7.181435,
    -8.872321, -4.213799, -5.463598, -7.8322635
]),
                                   dtype=tf.float32)

trained_gen_weights = tf.Variable(np.array([
    4.68485, -5.360671, -36.346577, -5.1716895, -10.068207, 7.2207055,
    -2.4580982, -36.35788, -1.0866196, 3.1072195, -36.354927, -36.34182,
    2.7561631, -36.35514, -36.35192, -36.353027, -36.341427, -1.7640233,
    4.3496346, -36.364895, -36.381893, 7.0489244
]),
                                  dtype=tf.float32)

state_vector = tfq.layers.State()(pure_gen,
                                  symbol_names=gs,
                                  symbol_values=tf.reshape(
                                      trained_gen_weights,
                                      (1, trained_gen_weights.shape[0])))
print(state_vector)
print(cirq.wavefunction_partial_trace_as_mixture(state_vector, [1, 2, 3]))
コード例 #4
0
def test_wavefunction_partial_trace_as_mixture_pure_result():
    a = cirq.testing.random_superposition(4)
    b = cirq.testing.random_superposition(8)
    c = cirq.testing.random_superposition(16)
    state = np.kron(np.kron(a, b), c).reshape((2, ) * 9)

    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [0, 1], atol=1e-8),
        ((1.0, a.reshape(2, 2)), ))
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [2, 3, 4],
                                                   atol=1e-8),
        ((1.0, b.reshape(2, 2, 2)), ))
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [5, 6, 7, 8],
                                                   atol=1e-8),
        ((1.0, c.reshape(2, 2, 2, 2)), ))
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [0, 1, 2, 3, 4],
                                                   atol=1e-8),
        ((1.0, np.kron(a, b).reshape((2, 2, 2, 2, 2))), ))
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [0, 1, 5, 6, 7, 8],
                                                   atol=1e-8),
        ((1.0, np.kron(a, c).reshape((2, 2, 2, 2, 2, 2))), ))
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state,
                                                   [2, 3, 4, 5, 6, 7, 8],
                                                   atol=1e-8),
        ((1.0, np.kron(b, c).reshape((2, 2, 2, 2, 2, 2, 2))), ))

    # Shapes of states in the output mixture conform to the input's shape.
    state = state.reshape(2**9)
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [0, 1], atol=1e-8),
        ((1.0, a), ))
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [2, 3, 4],
                                                   atol=1e-8), ((1.0, b), ))
    assert mixtures_equal(
        cirq.wavefunction_partial_trace_as_mixture(state, [5, 6, 7, 8],
                                                   atol=1e-8), ((1.0, c), ))

    # Return mixture will defer to numpy.linalg.eigh's builtin tolerance.
    state = np.array([1, 0, 0, 1]) / np.sqrt(2)
    truth = ((0.5, np.array([1, 0])), (0.5, np.array([0, 1])))
    assert mixtures_equal(cirq.wavefunction_partial_trace_as_mixture(
        state, [1], atol=1e-20),
                          truth,
                          atol=1e-15)
    assert not mixtures_equal(cirq.wavefunction_partial_trace_as_mixture(
        state, [1], atol=1e-20),
                              truth,
                              atol=1e-16)
コード例 #5
0
def test_wavefunction_partial_trace_as_mixture_invalid_input():

    with pytest.raises(ValueError, match='7'):
        cirq.wavefunction_partial_trace_as_mixture(np.arange(7), [1, 2],
                                                   atol=1e-8)

    bad_shape = np.arange(16).reshape((2, 4, 2))
    with pytest.raises(ValueError, match='shaped'):
        cirq.wavefunction_partial_trace_as_mixture(bad_shape, [1], atol=1e-8)
    bad_shape = np.arange(16).reshape((16, 1))
    with pytest.raises(ValueError, match='shaped'):
        cirq.wavefunction_partial_trace_as_mixture(bad_shape, [1], atol=1e-8)

    with pytest.raises(ValueError, match='normalized'):
        cirq.wavefunction_partial_trace_as_mixture(np.arange(8), [1],
                                                   atol=1e-8)

    state = np.arange(8) / np.linalg.norm(np.arange(8))
    with pytest.raises(ValueError, match='2, 2'):
        cirq.wavefunction_partial_trace_as_mixture(state, [1, 2, 2], atol=1e-8)

    state = np.array([1, 0, 0, 0]).reshape((2, 2))
    with pytest.raises(ValueError, match='invalid'):
        cirq.wavefunction_partial_trace_as_mixture(state, [5], atol=1e-8)
    with pytest.raises(ValueError, match='invalid'):
        cirq.wavefunction_partial_trace_as_mixture(state, [0, 1, 2], atol=1e-8)