예제 #1
0
def test_density_matrix_from_state_vector():
    test_state = np.array([
        0.0 - 0.35355339j,
        0.0 + 0.35355339j,
        0.0 - 0.35355339j,
        0.0 + 0.35355339j,
        0.0 + 0.35355339j,
        0.0 - 0.35355339j,
        0.0 + 0.35355339j,
        0.0 - 0.35355339j,
    ])

    full_rho = cirq.density_matrix_from_state_vector(test_state)
    np.testing.assert_array_almost_equal(
        full_rho, np.outer(test_state, np.conj(test_state)))

    rho_one = cirq.density_matrix_from_state_vector(test_state, [1])
    true_one = np.array([[0.5 + 0.0j, 0.5 + 0.0j], [0.5 + 0.0j, 0.5 + 0.0j]])
    np.testing.assert_array_almost_equal(rho_one, true_one)

    rho_two_zero = cirq.density_matrix_from_state_vector(test_state, [0, 2])
    true_two_zero = np.array([
        [0.25 + 0.0j, -0.25 + 0.0j, -0.25 + 0.0j, 0.25 + 0.0j],
        [-0.25 + 0.0j, 0.25 + 0.0j, 0.25 + 0.0j, -0.25 + 0.0j],
        [-0.25 + 0.0j, 0.25 + 0.0j, 0.25 + 0.0j, -0.25 + 0.0j],
        [0.25 + 0.0j, -0.25 + 0.0j, -0.25 + 0.0j, 0.25 + 0.0j],
    ])
    np.testing.assert_array_almost_equal(rho_two_zero, true_two_zero)

    # two and zero will have same single qubit density matrix.
    rho_two = cirq.density_matrix_from_state_vector(test_state, [2])
    true_two = np.array([[0.5 + 0.0j, -0.5 + 0.0j], [-0.5 + 0.0j, 0.5 + 0.0j]])
    np.testing.assert_array_almost_equal(rho_two, true_two)
    rho_zero = cirq.density_matrix_from_state_vector(test_state, [0])
    np.testing.assert_array_almost_equal(rho_zero, true_two)
예제 #2
0
def test_density_matrix_invalid():
    bad_state = np.array([0.5, 0.5, 0.5])
    good_state = np.array([0.5, 0.5, 0.5, 0.5])
    with pytest.raises(ValueError):
        _ = cirq.density_matrix_from_state_vector(bad_state)
    with pytest.raises(ValueError):
        _ = cirq.density_matrix_from_state_vector(bad_state, [0, 1])
    with pytest.raises(IndexError):
        _ = cirq.density_matrix_from_state_vector(good_state, [-1, 0, 1])
    with pytest.raises(IndexError):
        _ = cirq.density_matrix_from_state_vector(good_state, [-1])
예제 #3
0
def run_optimization_process():

    init = [random.randint(-600, 600)/100 for i in range(0, 4)]
    out = minimize(calculate_cost, x0=init, method="Nelder-Mead", options={'maxiter':500}, tol=1e-40)
    print(out)
    optimal_param = out['x']

    print("Optimal Parameters: "+str(optimal_param))

    final_final_state = qaoa_run(qubits_a, qubits_b, depth, qubit_number, [optimal_param[i] for i in range(0, 2)], [optimal_param[i] for i in range(2, 4)]).state_vector()

    density_matrix = cirq.density_matrix_from_state_vector(final_final_state)

    print("Optimal Final State: "+str(final_final_state))
    print("Probability Final State: "+str([np.conj(i)*i for i in list(final_final_state)]))

    norm = 0
    for i in list(final_final_state):
        norm = norm + float(i.real)**2
    norm = math.sqrt(norm)

    norm_state = [float(i.real/norm) for i in list(final_final_state)]

    print("Normalized Real: "+str(norm_state))

    good_state = construct_tfd_state(qubit_number, transverse_field_strength)
    print("Target State: "+str(good_state))

    good_density = cirq.density_matrix_from_state_vector(good_state)

    final_cost = np.inner(np.conj(good_state), final_final_state)*np.inner(np.conj(final_final_state), good_state)

    print("Final Cost: "+str(final_cost.real))

    final_cost_absolute = np.inner(good_state, np.array(norm_state))

    #np.inner(np.conj(np.array(norm_state)), good_state)

    print("The Absolute Final Cost: "+str(final_cost_absolute))

    return [density_matrix, good_density]
예제 #4
0
def test_deprecated():
    state_vector = np.array([1, 1], dtype=np.complex64) / np.sqrt(2)
    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.bloch_vector_from_state_vector(state=state_vector, index=0)

    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.density_matrix_from_state_vector(state=state_vector)

    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.dirac_notation(state=state_vector)

    with cirq.testing.assert_logs(
        'validate_normalized_state', 'validate_normalized_state_vector', 'deprecated'
    ):
        _ = cirq.validate_normalized_state(state_vector, qid_shape=(2,))

    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.validate_qid_shape(state=state_vector, qid_shape=(2,))