Пример #1
0
def test_deprecated():
    density_matrix = np.array([[0.5, 0], [0, 0.5]])
    with cirq.testing.assert_deprecated('density_matrix',
                                        'state',
                                        deadline="v0.11"):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.von_neumann_entropy(density_matrix=density_matrix)
Пример #2
0
def test_von_neumann_entropy():
    # 1x1 matrix
    assert cirq.von_neumann_entropy(np.array([[1]])) == 0
    # An EPR pair state (|00> + |11>)(<00| + <11|)
    assert (
        cirq.von_neumann_entropy(0.5 * np.array([1, 0, 0, 1] * np.array([[1], [0], [0], [1]]))) == 0
    )
    # Maximally mixed state
    # yapf: disable
    assert cirq.von_neumann_entropy(np.array(
        [[0.5, 0],
        [0, 0.5]])) == 1
    # yapf: enable
    # 2x2 random unitary, each column as a ket, each ket as a density matrix,
    # linear combination of the two with coefficients 0.1 and 0.9
    res = cirq.testing.random_unitary(2)
    first_column = res[:, 0]
    first_density_matrix = 0.1 * np.outer(first_column, np.conj(first_column))
    second_column = res[:, 1]
    second_density_matrix = 0.9 * np.outer(second_column, np.conj(second_column))
    assert np.isclose(
        cirq.von_neumann_entropy(first_density_matrix + second_density_matrix), 0.4689, atol=1e-04
    )

    assert np.isclose(
        cirq.von_neumann_entropy(np.diag([0, 0, 0.1, 0, 0.2, 0.3, 0.4, 0])), 1.8464, atol=1e-04
    )
    # Random NxN matrix
    probs = np.random.exponential(size=N)
    probs /= np.sum(probs)
    mat = U @ (probs * U).T.conj()

    np.testing.assert_allclose(
        cirq.von_neumann_entropy(mat), -np.sum(probs * np.log(probs) / np.log(2))
    )
    # QuantumState object
    assert (
        cirq.von_neumann_entropy(cirq.quantum_state(np.array([[0.5, 0], [0, 0.5]]), qid_shape=(2,)))
        == 1
    )
    assert (
        cirq.von_neumann_entropy(
            cirq.quantum_state(np.array([[0.5, 0.5], [0.5, 0.5]]), qid_shape=(2, 2))
        )
        == 0
    )
Пример #3
0
def test_von_neumann_entropy():
    # 1x1 matrix
    assert cirq.von_neumann_entropy(np.array([[1]])) == 0
    # An EPR pair state (|00> + |11>)(<00| + <11|)
    assert cirq.von_neumann_entropy(
        np.array([1, 0, 0, 1] * np.array([[1], [0], [0], [1]]))) == 0
    # Maximally mixed state
    # yapf: disable
    assert cirq.von_neumann_entropy(np.array(
        [[0.5, 0],
        [0, 0.5]])) == 1
    # 3x3 state
    assert np.isclose(cirq.von_neumann_entropy(
        np.array(
            [[0.5, 0.5j, 1],
            [-0.5j, 0.5, 0],
            [0.7, 0.4, 0.6]])),
                      1.37,
                      atol=1e-01)
    # 4X4 state
    assert np.isclose(cirq.von_neumann_entropy(
        np.array(
            [[0.5, 0.5j, 1, 3],
            [-0.5j, 0.5, 0, 4],
            [0.7, 0.4, 0.6, 5],
            [6, 7, 8, 9]])),
                      1.12,
                      atol=1e-01)
    # yapf: enable
    # 2x2 random unitary, each column as a ket, each ket as a density matrix,
    # linear combination of the two with coefficients 0.1 and 0.9
    res = cirq.testing.random_unitary(2)
    first_column = res[:, 0]
    first_density_matrix = 0.1 * np.outer(first_column, np.conj(first_column))
    second_column = res[:, 1]
    second_density_matrix = 0.9 * np.outer(second_column,
                                           np.conj(second_column))
    assert np.isclose(cirq.von_neumann_entropy(first_density_matrix +
                                               second_density_matrix),
                      0.4689,
                      atol=1e-04)

    assert np.isclose(cirq.von_neumann_entropy(
        np.diag([0, 0, 0.1, 0, 0.2, 0.3, 0.4, 0])),
                      1.8464,
                      atol=1e-04)