Пример #1
0
def test_too_much_ram():
    qubits = cirq.LineQubit.range(30)
    circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=20, op_density=0.8)
    op = functools.reduce(operator.mul, [cirq.Z(q) for q in qubits], 1)
    with pytest.raises(MemoryError) as e:
        ccq.tensor_expectation_value(circuit=circuit, pauli_string=op)

    assert e.match(r'.*too much RAM!.*')
Пример #2
0
def test_tensor_expectation_value():
    for _ in range(10):
        for width in [2, 3]:
            for height in [1, 3]:
                for p in [1, 2]:
                    rs = np.random.RandomState(52)
                    circuit, qubits = _get_circuit(width=width,
                                                   height=height,
                                                   p=p,
                                                   rs=rs)
                    operator = cirq.PauliString(
                        {
                            q: cirq.Z
                            for q in rs.choice(qubits, size=2, replace=False)
                        },
                        coefficient=rs.uniform(-1, 1),
                    )

                    eval_tn = ccq.tensor_expectation_value(circuit, operator)

                    wfn = cirq.Simulator().simulate(circuit)
                    eval_normal = operator.expectation_from_state_vector(
                        wfn.final_state_vector, wfn.qubit_map)
                    assert eval_normal.imag < 1e-6
                    eval_normal = eval_normal.real
                    np.testing.assert_allclose(eval_tn, eval_normal, atol=1e-3)
Пример #3
0
def test_tensor_expectation_value():
    rs = np.random.RandomState(52)
    for _ in range(10):
        for n_qubit in [2, 7]:
            qubits = cirq.LineQubit.range(n_qubit)
            for depth in [10, 20]:
                circuit = cirq.testing.random_circuit(
                    qubits=qubits, n_moments=depth, op_density=0.8, random_state=rs
                )
                operator = _random_pauli_string(qubits, rs, coefficients=True)
                eval_tn = ccq.tensor_expectation_value(circuit, operator)

                wfn = cirq.Simulator().simulate(circuit)
                eval_normal = operator.expectation_from_state_vector(
                    wfn.final_state_vector, wfn.qubit_map
                )
                assert eval_normal.imag < 1e-6
                eval_normal = eval_normal.real
                np.testing.assert_allclose(eval_tn, eval_normal, atol=1e-3)