Exemplo n.º 1
0
def test_circuit_to_tensors(simplify):
    rs = np.random.RandomState(52)
    qubits = cirq.LineQubit.range(2)
    circuit = cirq.testing.random_circuit(qubits=qubits,
                                          n_moments=10,
                                          op_density=0.8)
    operator = cirq.PauliString(
        {q: cirq.Z for q in rs.choice(qubits, size=2, replace=False)})

    circuit_sand = ccq.circuit_for_expectation_value(circuit, operator)
    if simplify:
        ccq.simplify_expectation_value_circuit(circuit_sand)
    qubits = sorted(circuit_sand.all_qubits())
    u_tn = ccq.tensor_unitary(circuit=circuit_sand, qubits=qubits)
    u_cirq = cirq.unitary(circuit_sand)
    np.testing.assert_allclose(u_tn, u_cirq, atol=1e-6)
Exemplo n.º 2
0
def test_simplify_sandwich():
    rs = np.random.RandomState(52)
    for width in [2, 3]:
        for height in [1, 3]:
            for p in [1, 2]:
                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)
                })
                tot_c = ccq.circuit_for_expectation_value(circuit, operator)
                tot_c_init = tot_c.copy()
                ccq.simplify_expectation_value_circuit(tot_c)
                assert len(list(tot_c.all_operations())) < len(
                    list(tot_c_init.all_operations()))
                np.testing.assert_allclose(
                    tot_c.unitary(qubit_order=qubits),
                    tot_c_init.unitary(qubit_order=qubits),
                    atol=1e-5)