def test_sandwich_operator_identity(): qubits = cirq.LineQubit.range(6) circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=10, op_density=0.8) tot_c = ccq.circuit_for_expectation_value(circuit, cirq.PauliString({})) np.testing.assert_allclose(cirq.unitary(tot_c), np.eye(2**len(qubits)), atol=1e-6)
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_sandwich_operator_expect_val(): rs = np.random.RandomState(52) qubits = cirq.LineQubit.range(5) for _ in range(10): # try a bunch of different ones circuit = cirq.testing.random_circuit( qubits=qubits, n_moments=10, op_density=0.8, random_state=rs ) operator = _random_pauli_string(qubits, rs) tot_c = ccq.circuit_for_expectation_value(circuit, operator) eval_sandwich = cirq.unitary(tot_c)[0, 0] wfn = cirq.Simulator().simulate(circuit) eval_normal = operator.expectation_from_state_vector(wfn.final_state_vector, wfn.qubit_map) np.testing.assert_allclose(eval_sandwich, eval_normal, atol=1e-5)
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)
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)