Exemple #1
0
def test_tensor_unitary():
    rs = np.random.RandomState(52)
    for _ in range(10):
        qubits = cirq.LineQubit.range(5)
        circuit = cirq.testing.random_circuit(
            qubits=qubits, n_moments=10, op_density=0.8, random_state=rs
        )
        operator = _random_pauli_string(qubits, rs)

        circuit_sand = ccq.circuit_for_expectation_value(circuit, operator)
        u_tn = ccq.tensor_unitary(circuit_sand, qubits)
        u_cirq = cirq.unitary(circuit_sand)
        np.testing.assert_allclose(u_tn, u_cirq, atol=1e-6)
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)