def test_exact_expectation_values_without_wavefunction_simulator( self, backend): if backend.device_name != "wavefunction-simulator": operator = QubitOperator("Z0 Z1") circuit = Circuit([X(0), X(1)]) with pytest.raises(Exception): backend.get_exact_expectation_values(circuit, operator)
def test_build_hartree_fock_circuit_jordan_wigner(self): number_of_qubits = 4 number_of_alpha_electrons = 1 number_of_beta_electrons = 1 transformation = "Jordan-Wigner" expected_circuit = Circuit([X(0), X(1)], n_qubits=number_of_qubits) actual_circuit = build_hartree_fock_circuit( number_of_qubits, number_of_alpha_electrons, number_of_beta_electrons, transformation, ) assert actual_circuit == expected_circuit
def test_group_greedily_all_different_groups(self): target_operator = 10.0 * QubitOperator("Z0") target_operator -= 3.0 * QubitOperator("Y0") target_operator += 1.0 * QubitOperator("X0") target_operator += 20.0 * QubitOperator("") expected_operators = [ 10.0 * QubitOperator("Z0"), -3.0 * QubitOperator("Y0"), 1.0 * QubitOperator("X0"), 20.0 * QubitOperator(""), ] circuit = Circuit([X(0)]) estimation_tasks = [EstimationTask(target_operator, circuit, None)] grouped_tasks = group_greedily(estimation_tasks) for task, operator in zip(grouped_tasks, expected_operators): assert task.operator == operator for initial_task, modified_task in zip(estimation_tasks, grouped_tasks): assert modified_task.circuit == initial_task.circuit assert modified_task.number_of_shots == initial_task.number_of_shots
def test_group_individually(self): target_operator = 10.0 * QubitOperator("Z0") target_operator += 5.0 * QubitOperator("Z1") target_operator -= 3.0 * QubitOperator("Y0") target_operator += 1.0 * QubitOperator("X0") target_operator += 20.0 * QubitOperator("") expected_operator_terms_per_frame = [ (10.0 * QubitOperator("Z0")).terms, (5.0 * QubitOperator("Z1")).terms, (-3.0 * QubitOperator("Y0")).terms, (1.0 * QubitOperator("X0")).terms, (20.0 * QubitOperator("")).terms, ] circuit = Circuit([X(0)]) estimation_tasks = [EstimationTask(target_operator, circuit, None)] grouped_tasks = group_individually(estimation_tasks) assert len(grouped_tasks) == 5 for task in grouped_tasks: assert task.operator.terms in expected_operator_terms_per_frame
def test_group_greedily_all_comeasureable(self): target_operator = 10.0 * QubitOperator("Y0") target_operator -= 3.0 * QubitOperator("Y0 Y1") target_operator += 1.0 * QubitOperator("Y1") target_operator += 20.0 * QubitOperator("Y0 Y1 Y2") circuit = Circuit([X(0), X(1), X(2)]) estimation_tasks = [EstimationTask(target_operator, circuit, None)] grouped_tasks = group_greedily(estimation_tasks) assert len(grouped_tasks) == 1 assert grouped_tasks[0].operator == target_operator for initial_task, modified_task in zip(estimation_tasks, grouped_tasks): assert modified_task.circuit == initial_task.circuit assert modified_task.number_of_shots == initial_task.number_of_shots
def test_build_hartree_fock_circuit_bravyi_kitaev(self): number_of_qubits = 4 number_of_alpha_electrons = 1 number_of_beta_electrons = 1 transformation = "Bravyi-Kitaev" expected_circuit = Circuit([X(0)], n_qubits=number_of_qubits) actual_circuit = build_hartree_fock_circuit( number_of_qubits, number_of_alpha_electrons, number_of_beta_electrons, transformation, ) assert actual_circuit == expected_circuit
def test_create_circuits_from_qubit_operator(self): # Initialize target circuit1 = Circuit([Z(0), X(1)]) circuit2 = Circuit([Y(0), Z(1)]) # Given qubit_op = QubitOperator("Z0 X1") + QubitOperator("Y0 Z1") # When pauli_circuits = create_circuits_from_qubit_operator(qubit_op) # Then self.assertEqual(pauli_circuits[0], circuit1) self.assertEqual(pauli_circuits[1], circuit2)
def estimation_tasks(self): task_1 = EstimationTask(IsingOperator("Z0"), circuit=Circuit([X(0)]), number_of_shots=10) task_2 = EstimationTask( IsingOperator("Z0"), circuit=Circuit([RY(np.pi / 2)(0)]), number_of_shots=20, ) task_3 = EstimationTask( IsingOperator((), coefficient=2.0), circuit=Circuit([RY(np.pi / 4)(0)]), number_of_shots=30, ) return [task_1, task_2, task_3]
def test_commutes_with_parameter_substitution(self): theta, gamma = sympy.symbols("theta, gamma") circuit = Circuit([ RX(theta / 2)(0), X(1), RY(gamma / 4).controlled(1)(1, 2), YY(0.1)(0, 4) ]) symbols_map = {theta: 0.1, gamma: 0.5} parameterized_unitary = circuit.to_unitary() unitary = circuit.bind(symbols_map).to_unitary() np.testing.assert_array_almost_equal( np.array(parameterized_unitary.subs(symbols_map), dtype=complex), unitary)
def test_run_circuitset_and_measure_n_samples(self, backend): # We override the base test because the qiskit integration may return # more samples than requested due to the fact that each circuit in a # batch must have the same number of measurements. # Note: this test may fail with noisy devices # Given backend.number_of_circuits_run = 0 backend.number_of_jobs_run = 0 first_circuit = Circuit([ X(0), X(0), X(1), X(1), X(2), ]) second_circuit = Circuit([ X(0), X(1), X(2), ]) n_samples = [100, 105] # When backend.n_samples = n_samples measurements_set = backend.run_circuitset_and_measure( [first_circuit, second_circuit], n_samples) # Then (since SPAM error could result in unexpected bitstrings, we make sure the # most common bitstring is the one we expect) counts = measurements_set[0].get_counts() assert max(counts, key=counts.get) == "001" counts = measurements_set[1].get_counts() assert max(counts, key=counts.get) == "111" assert len(measurements_set[0].bitstrings) >= n_samples[0] assert len(measurements_set[1].bitstrings) >= n_samples[1] assert backend.number_of_circuits_run == 2
class TestDecompositionOfU3Gates: @pytest.mark.parametrize( "gate_to_be_decomposed, target_qubits", [ *[(gate, qubits) for gate in U3_GATES for qubits in [(0,), (2,)]], ], ) def test_gives_the_same_unitary_as_original_gate_up_to_global_phase( self, gate_to_be_decomposed, target_qubits ): circuit = Circuit([gate_to_be_decomposed(*target_qubits)]) decomposed_circuit = decompose_zquantum_circuit(circuit, DECOMPOSITION_RULES) assert _is_scaled_identity( circuit.to_unitary() @ np.linalg.inv(decomposed_circuit.to_unitary()), ) @pytest.mark.parametrize( "operations", [[RY(np.pi / 2)(0)], [X(3), Y(1), Z(0)], [CNOT(3, 11)]], ) def test_leaves_gates_not_matching_predicate_unaffected(self, operations): circuit = Circuit(operations) decomposed_circuit = decompose_zquantum_circuit(circuit, DECOMPOSITION_RULES) assert circuit.operations == decomposed_circuit.operations @pytest.mark.parametrize("target_qubits", [(0,), (2,)]) @pytest.mark.parametrize("gate_to_be_decomposed", U3_GATES) def test_U3_decomposition_comprises_only_rotations( self, gate_to_be_decomposed, target_qubits ): circuit = Circuit([gate_to_be_decomposed(*target_qubits)]) decomposed_circuit = decompose_zquantum_circuit(circuit, DECOMPOSITION_RULES) assert all( isinstance(op, GateOperation) and op.gate.name in ("RZ", "RY") for op in decomposed_circuit.operations )
def test_perform_context_selection(self): target_operators = [] target_operators.append(10.0 * QubitOperator("Z0")) target_operators.append(-3 * QubitOperator("Y0")) target_operators.append(1 * QubitOperator("X0")) target_operators.append(20 * QubitOperator("")) expected_operators = [] expected_operators.append(10.0 * QubitOperator("Z0")) expected_operators.append(-3 * QubitOperator("Z0")) expected_operators.append(1 * QubitOperator("Z0")) expected_operators.append(20 * QubitOperator("")) base_circuit = Circuit([X(0)]) x_term_circuit = Circuit([RY(-np.pi / 2)(0)]) y_term_circuit = Circuit([RX(np.pi / 2)(0)]) expected_circuits = [ base_circuit, base_circuit + y_term_circuit, base_circuit + x_term_circuit, base_circuit, ] estimation_tasks = [ EstimationTask(operator, base_circuit, None) for operator in target_operators ] tasks_with_context_selection = perform_context_selection( estimation_tasks) for task, expected_circuit, expected_operator in zip( tasks_with_context_selection, expected_circuits, expected_operators): assert task.operator.terms == expected_operator.terms assert task.circuit == expected_circuit
class TestEstimatorUtils: def test_get_context_selection_circuit_for_group(self): group = QubitOperator("X0 Y1") - 0.5 * QubitOperator((1, "Y")) circuit, ising_operator = get_context_selection_circuit_for_group( group) # Need to convert to QubitOperator in order to get matrix representation qubit_operator = change_operator_type(ising_operator, QubitOperator) target_unitary = qubit_operator_sparse(group) transformed_unitary = ( circuit.to_unitary().conj().T @ qubit_operator_sparse(qubit_operator) @ circuit.to_unitary()) assert np.allclose(target_unitary.todense(), transformed_unitary) def test_perform_context_selection(self): target_operators = [] target_operators.append(10.0 * QubitOperator("Z0")) target_operators.append(-3 * QubitOperator("Y0")) target_operators.append(1 * QubitOperator("X0")) target_operators.append(20 * QubitOperator("")) expected_operators = [] expected_operators.append(10.0 * QubitOperator("Z0")) expected_operators.append(-3 * QubitOperator("Z0")) expected_operators.append(1 * QubitOperator("Z0")) expected_operators.append(20 * QubitOperator("")) base_circuit = Circuit([X(0)]) x_term_circuit = Circuit([RY(-np.pi / 2)(0)]) y_term_circuit = Circuit([RX(np.pi / 2)(0)]) expected_circuits = [ base_circuit, base_circuit + y_term_circuit, base_circuit + x_term_circuit, base_circuit, ] estimation_tasks = [ EstimationTask(operator, base_circuit, None) for operator in target_operators ] tasks_with_context_selection = perform_context_selection( estimation_tasks) for task, expected_circuit, expected_operator in zip( tasks_with_context_selection, expected_circuits, expected_operators): assert task.operator.terms == expected_operator.terms assert task.circuit == expected_circuit @pytest.fixture() def frame_operators(self): operators = [ 2.0 * IsingOperator((1, "Z")) * IsingOperator((2, "Z")), 1.0 * IsingOperator((3, "Z")) * IsingOperator((0, "Z")), -1.0 * IsingOperator((2, "Z")), ] return operators @pytest.fixture() def circuits(self): circuits = [Circuit() for _ in range(5)] circuits[1] += RX(1.2)(0) circuits[1] += RY(1.5)(1) circuits[1] += RX(-0.0002)(0) circuits[1] += RY(0)(1) for circuit in circuits[2:]: circuit += RX(sympy.Symbol("theta_0"))(0) circuit += RY(sympy.Symbol("theta_1"))(1) circuit += RX(sympy.Symbol("theta_2"))(0) circuit += RY(sympy.Symbol("theta_3"))(1) return circuits @pytest.mark.parametrize( "n_samples, target_n_samples_list", [ (100, [100, 100, 100]), (17, [17, 17, 17]), ], ) def test_allocate_shots_uniformly( self, frame_operators, n_samples, target_n_samples_list, ): allocate_shots = partial(allocate_shots_uniformly, number_of_shots=n_samples) circuit = Circuit() estimation_tasks = [ EstimationTask(operator, circuit, 1) for operator in frame_operators ] new_estimation_tasks = allocate_shots(estimation_tasks) for task, target_n_samples in zip(new_estimation_tasks, target_n_samples_list): assert task.number_of_shots == target_n_samples @pytest.mark.parametrize( "total_n_shots, prior_expectation_values, target_n_samples_list", [ (400, None, [200, 100, 100]), (400, ExpectationValues(np.array([0, 0, 0])), [200, 100, 100]), (400, ExpectationValues(np.array([1, 0.3, 0.3])), [0, 200, 200]), ], ) def test_allocate_shots_proportionally( self, frame_operators, total_n_shots, prior_expectation_values, target_n_samples_list, ): allocate_shots = partial( allocate_shots_proportionally, total_n_shots=total_n_shots, prior_expectation_values=prior_expectation_values, ) circuit = Circuit() estimation_tasks = [ EstimationTask(operator, circuit, 1) for operator in frame_operators ] new_estimation_tasks = allocate_shots(estimation_tasks) for task, target_n_samples in zip(new_estimation_tasks, target_n_samples_list): assert task.number_of_shots == target_n_samples @pytest.mark.parametrize( "n_samples", [-1], ) def test_allocate_shots_uniformly_invalid_inputs( self, n_samples, ): estimation_tasks = [] with pytest.raises(ValueError): allocate_shots_uniformly(estimation_tasks, number_of_shots=n_samples) @pytest.mark.parametrize( "total_n_shots, prior_expectation_values", [ (-1, ExpectationValues(np.array([0, 0, 0]))), ], ) def test_allocate_shots_proportionally_invalid_inputs( self, total_n_shots, prior_expectation_values, ): estimation_tasks = [] with pytest.raises(ValueError): _ = allocate_shots_proportionally(estimation_tasks, total_n_shots, prior_expectation_values) def test_evaluate_estimation_circuits_no_symbols( self, circuits, ): evaluate_circuits = partial(evaluate_estimation_circuits, symbols_maps=[[] for _ in circuits]) operator = QubitOperator() estimation_tasks = [ EstimationTask(operator, circuit, 1) for circuit in circuits ] new_estimation_tasks = evaluate_circuits(estimation_tasks) for old_task, new_task in zip(estimation_tasks, new_estimation_tasks): assert old_task.circuit == new_task.circuit def test_evaluate_estimation_circuits_all_symbols( self, circuits, ): symbols_maps = [[ (sympy.Symbol("theta_0"), 0), (sympy.Symbol("theta_1"), 0), (sympy.Symbol("theta_2"), 0), (sympy.Symbol("theta_3"), 0), ] for _ in circuits] evaluate_circuits = partial( evaluate_estimation_circuits, symbols_maps=symbols_maps, ) operator = QubitOperator() estimation_tasks = [ EstimationTask(operator, circuit, 1) for circuit in circuits ] new_estimation_tasks = evaluate_circuits(estimation_tasks) for new_task in new_estimation_tasks: assert len(new_task.circuit.free_symbols) == 0 def test_group_greedily_all_different_groups(self): target_operator = 10.0 * QubitOperator("Z0") target_operator -= 3.0 * QubitOperator("Y0") target_operator += 1.0 * QubitOperator("X0") target_operator += 20.0 * QubitOperator("") expected_operators = [ 10.0 * QubitOperator("Z0"), -3.0 * QubitOperator("Y0"), 1.0 * QubitOperator("X0"), 20.0 * QubitOperator(""), ] circuit = Circuit([X(0)]) estimation_tasks = [EstimationTask(target_operator, circuit, None)] grouped_tasks = group_greedily(estimation_tasks) for task, operator in zip(grouped_tasks, expected_operators): assert task.operator == operator for initial_task, modified_task in zip(estimation_tasks, grouped_tasks): assert modified_task.circuit == initial_task.circuit assert modified_task.number_of_shots == initial_task.number_of_shots def test_group_greedily_all_comeasureable(self): target_operator = 10.0 * QubitOperator("Y0") target_operator -= 3.0 * QubitOperator("Y0 Y1") target_operator += 1.0 * QubitOperator("Y1") target_operator += 20.0 * QubitOperator("Y0 Y1 Y2") circuit = Circuit([X(0), X(1), X(2)]) estimation_tasks = [EstimationTask(target_operator, circuit, None)] grouped_tasks = group_greedily(estimation_tasks) assert len(grouped_tasks) == 1 assert grouped_tasks[0].operator == target_operator for initial_task, modified_task in zip(estimation_tasks, grouped_tasks): assert modified_task.circuit == initial_task.circuit assert modified_task.number_of_shots == initial_task.number_of_shots def test_group_individually(self): target_operator = 10.0 * QubitOperator("Z0") target_operator += 5.0 * QubitOperator("Z1") target_operator -= 3.0 * QubitOperator("Y0") target_operator += 1.0 * QubitOperator("X0") target_operator += 20.0 * QubitOperator("") expected_operator_terms_per_frame = [ (10.0 * QubitOperator("Z0")).terms, (5.0 * QubitOperator("Z1")).terms, (-3.0 * QubitOperator("Y0")).terms, (1.0 * QubitOperator("X0")).terms, (20.0 * QubitOperator("")).terms, ] circuit = Circuit([X(0)]) estimation_tasks = [EstimationTask(target_operator, circuit, None)] grouped_tasks = group_individually(estimation_tasks) assert len(grouped_tasks) == 5 for task in grouped_tasks: assert task.operator.terms in expected_operator_terms_per_frame @pytest.mark.parametrize( ",".join([ "estimation_tasks", "ref_estimation_tasks_to_measure", "ref_non_measured_estimation_tasks", "ref_indices_to_measure", "ref_non_measured_indices", ]), [ ( [ EstimationTask(IsingOperator("2[Z0] + 3 [Z1 Z2]"), Circuit([X(0)]), 10), EstimationTask( IsingOperator("2[Z0] + 3 [Z1 Z2] + 4[]"), Circuit([RZ(np.pi / 2)(0)]), 1000, ), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 17, ), ], [ EstimationTask(IsingOperator("2[Z0] + 3 [Z1 Z2]"), Circuit([X(0)]), 10), EstimationTask( IsingOperator("2[Z0] + 3 [Z1 Z2] + 4 []"), Circuit([RZ(np.pi / 2)(0)]), 1000, ), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 17, ), ], [], [0, 1, 2], [], ), ( [ EstimationTask(IsingOperator("2[Z0] + 3 [Z1 Z2]"), Circuit([X(0)]), 10), EstimationTask( IsingOperator("4[] "), Circuit([RZ(np.pi / 2)(0)]), 1000, ), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 17, ), ], [ EstimationTask(IsingOperator("2[Z0] + 3 [Z1 Z2]"), Circuit([X(0)]), 10), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 17, ), ], [ EstimationTask(IsingOperator("4[]"), Circuit([RZ(np.pi / 2)(0)]), 1000) ], [0, 2], [1], ), ( [ EstimationTask(IsingOperator("- 3 []"), Circuit([X(0)]), 0), EstimationTask( IsingOperator("2[Z0] + 3 [Z1 Z2] + 4[]"), Circuit([RZ(np.pi / 2)(0)]), 1000, ), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 17, ), ], [ EstimationTask( IsingOperator("2[Z0] + 3 [Z1 Z2] + 4 []"), Circuit([RZ(np.pi / 2)(0)]), 1000, ), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 17, ), ], [ EstimationTask(IsingOperator("- 3 []"), Circuit([X(0)]), 0), ], [1, 2], [0], ), ( [ EstimationTask(IsingOperator("- 3 []"), Circuit([X(0)]), 0), EstimationTask( IsingOperator("2[Z0] + 3 [Z1 Z2] + 4[]"), Circuit([RZ(np.pi / 2)(0)]), 1000, ), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 0, ), ], [ EstimationTask( IsingOperator("2[Z0] + 3 [Z1 Z2] + 4 []"), Circuit([RZ(np.pi / 2)(0)]), 1000, ), ], [ EstimationTask(IsingOperator("- 3 []"), Circuit([X(0)]), 0), EstimationTask( IsingOperator("4[Z3]"), Circuit([RY(np.pi / 2)(0)]), 0, ), ], [1], [0, 2], ), ], ) def test_split_estimation_tasks_to_measure( self, estimation_tasks, ref_estimation_tasks_to_measure, ref_non_measured_estimation_tasks, ref_indices_to_measure, ref_non_measured_indices, ): ( estimation_task_to_measure, non_measured_estimation_tasks, indices_to_measure, indices_for_non_measureds, ) = split_estimation_tasks_to_measure(estimation_tasks) assert estimation_task_to_measure == ref_estimation_tasks_to_measure assert non_measured_estimation_tasks == ref_non_measured_estimation_tasks assert indices_to_measure == ref_indices_to_measure assert ref_non_measured_indices == indices_for_non_measureds @pytest.mark.parametrize( "estimation_tasks,ref_expectation_values", [ ( [ EstimationTask( IsingOperator("4[] "), Circuit([RZ(np.pi / 2)(0)]), 1000, ), ], [ ExpectationValues( np.asarray([4.0]), correlations=[np.asarray([[0.0]])], estimator_covariances=[np.asarray([[0.0]])], ), ], ), ( [ EstimationTask(IsingOperator("- 2.5 [] - 0.5 []"), Circuit([X(0)]), 0), EstimationTask(IsingOperator("0.001[] "), Circuit([RZ(np.pi / 2)(0)]), 2), EstimationTask( IsingOperator("2.5 [Z1] + 1.0 [Z2 Z3]"), Circuit([RY(np.pi / 2)(0)]), 0, ), ], [ ExpectationValues( np.asarray([-3.0]), correlations=[np.asarray([[0.0]])], estimator_covariances=[np.asarray([[0.0]])], ), ExpectationValues( np.asarray([0.001]), correlations=[np.asarray([[0.0]])], estimator_covariances=[np.asarray([[0.0]])], ), ExpectationValues( np.asarray([0.0]), correlations=[np.asarray([[0.0]])], estimator_covariances=[np.asarray([[0.0]])], ), ], ), ], ) def test_evaluate_non_measured_estimation_tasks(self, estimation_tasks, ref_expectation_values): expectation_values = evaluate_non_measured_estimation_tasks( estimation_tasks) for ex_val, ref_ex_val in zip(expectation_values, ref_expectation_values): assert np.allclose(ex_val.values, ref_ex_val.values) assert np.allclose(ex_val.correlations, ref_ex_val.correlations) assert np.allclose(ex_val.estimator_covariances, ref_ex_val.estimator_covariances) @pytest.mark.parametrize( "estimation_tasks", [ ([ EstimationTask(IsingOperator("- 2.5 [] - 0.5 [Z1]"), Circuit([X(0)]), 1), ]), ([ EstimationTask( IsingOperator("0.001 [Z0]"), Circuit([RZ(np.pi / 2)(0)]), 0, ), EstimationTask(IsingOperator("2.0[] "), Circuit([RZ(np.pi / 2)(0)]), 2), EstimationTask( IsingOperator("1.5 [Z0 Z1]"), Circuit([RY(np.pi / 2)(0)]), 10, ), ]), ], ) def test_evaluate_non_measured_estimation_tasks_fails_with_non_zero_shots( self, estimation_tasks): with pytest.raises(RuntimeError): _ = evaluate_non_measured_estimation_tasks(estimation_tasks)
class TestCreatingUnitaryFromCircuit: @pytest.fixture def cirq_unitaries(self): """ Note: We decided to go with file-based approach after extracting cirq from z-quantum-core and not being able to use `export_to_cirq` anymore. """ path_to_array = ("/".join(__file__.split("/")[:-1]) + "/hardcoded_cirq_unitaries.npy") return np.load(path_to_array, allow_pickle=True) @pytest.mark.parametrize( "circuit, unitary_index", [ # Identity gates in some test cases below are used so that comparable # Cirq circuits have the same number of qubits as Zquantum ones. (Circuit([RX(np.pi / 5)(0)]), 0), (Circuit([RY(np.pi / 2)(0), RX(np.pi / 5)(0)]), 1), ( Circuit( [I(1), I(2), I(3), I(4), RX(np.pi / 5)(0), XX(0.1)(5, 0)]), 2, ), ( Circuit([ XY(np.pi).controlled(1)(3, 1, 4), RZ(0.1 * np.pi).controlled(2)(0, 2, 1), ]), 3, ), ( Circuit( [H(1), YY(0.1).controlled(1)(0, 1, 2), X(2), Y(3), Z(4)]), 4, ), ], ) def test_without_free_params_gives_the_same_result_as_cirq( self, circuit, unitary_index, cirq_unitaries): zquantum_unitary = circuit.to_unitary() assert isinstance( zquantum_unitary, np.ndarray ), "Unitary constructed from non-parameterized circuit is not a numpy array." np.testing.assert_array_almost_equal(zquantum_unitary, cirq_unitaries[unitary_index]) def test_commutes_with_parameter_substitution(self): theta, gamma = sympy.symbols("theta, gamma") circuit = Circuit([ RX(theta / 2)(0), X(1), RY(gamma / 4).controlled(1)(1, 2), YY(0.1)(0, 4) ]) symbols_map = {theta: 0.1, gamma: 0.5} parameterized_unitary = circuit.to_unitary() unitary = circuit.bind(symbols_map).to_unitary() np.testing.assert_array_almost_equal( np.array(parameterized_unitary.subs(symbols_map), dtype=complex), unitary)
10, ), ]), ], ) def test_evaluate_non_measured_estimation_tasks_fails_with_non_zero_shots( self, estimation_tasks): with pytest.raises(RuntimeError): _ = evaluate_non_measured_estimation_tasks(estimation_tasks) TEST_CASES_EIGENSTATES = [ ( [ EstimationTask(IsingOperator("Z0"), circuit=Circuit([X(0)]), number_of_shots=10), EstimationTask( IsingOperator((), coefficient=2.0), circuit=Circuit([RY(np.pi / 4)(0)]), number_of_shots=30, ), ], [ExpectationValues(np.array([-1])), ExpectationValues(np.array([2]))], ), ] TEST_CASES_NONEIGENSTATES = [ ( [ EstimationTask(
def circuit(self): return Circuit([X(0)])
def x_cnot_circuit(self): circuit = Circuit([X(0), CNOT(1, 2)]) return circuit