def test_qae_circuit(self, efficient_circuit): """Test circuits resulting from canonical amplitude estimation. Build the circuit manually and from the algorithm and compare the resulting unitaries. """ prob = 0.5 problem = EstimationProblem(BernoulliStateIn(prob), objective_qubits=[0]) for m in [2, 5]: qae = AmplitudeEstimation(m) angle = 2 * np.arcsin(np.sqrt(prob)) # manually set up the inefficient AE circuit qr_eval = QuantumRegister(m, "a") qr_objective = QuantumRegister(1, "q") circuit = QuantumCircuit(qr_eval, qr_objective) # initial Hadamard gates for i in range(m): circuit.h(qr_eval[i]) # A operator circuit.ry(angle, qr_objective) if efficient_circuit: qae.grover_operator = BernoulliGrover(prob) for power in range(m): circuit.cry(2 * 2 ** power * angle, qr_eval[power], qr_objective[0]) else: oracle = QuantumCircuit(1) oracle.z(0) state_preparation = QuantumCircuit(1) state_preparation.ry(angle, 0) grover_op = GroverOperator(oracle, state_preparation) grover_op.global_phase = np.pi for power in range(m): circuit.compose( grover_op.power(2 ** power).control(), qubits=[qr_eval[power], qr_objective[0]], inplace=True, ) # fourier transform iqft = QFT(m, do_swaps=False).inverse().reverse_bits() circuit.append(iqft.to_instruction(), qr_eval) actual_circuit = qae.construct_circuit(problem, measurement=False) self.assertEqual(Operator(circuit), Operator(actual_circuit))
def test_construct_circuit(self): """Test construct_circuit""" oracle = QuantumCircuit(2) oracle.cz(0, 1) problem = AmplificationProblem(oracle, is_good_state=["11"]) grover = Grover() constructed = grover.construct_circuit(problem, 2, measurement=False) grover_op = GroverOperator(oracle) expected = QuantumCircuit(2) expected.h([0, 1]) expected.compose(grover_op.power(2), inplace=True) self.assertTrue(Operator(constructed).equiv(Operator(expected)))
class TestGroverFunctionality(QiskitAquaTestCase): """Test for the functionality of Grover""" def setUp(self): super().setUp() self._oracle = Statevector.from_label('111') self._expected_grover_op = GroverOperator(oracle=self._oracle) self._expected = QuantumCircuit(self._expected_grover_op.num_qubits) self._expected.compose(self._expected_grover_op.state_preparation, inplace=True) self._expected.compose(self._expected_grover_op.power(2), inplace=True) backend = BasicAer.get_backend('statevector_simulator') self._sv = QuantumInstance(backend) def test_num_iteration(self): """Test specified num_iterations""" # filtering the following: # DeprecationWarning: The num_iterations argument is deprecated as of 0.8.0, # and will be removed no earlier than 3 months after the release date. # If you want to use the num_iterations argument you should use the iterations # argument instead and pass an integer for the number of iterations. try: warnings.filterwarnings(action="ignore", category=DeprecationWarning) grover = Grover(oracle=self._oracle, good_state=['111'], num_iterations=2) finally: warnings.filterwarnings(action="always", category=DeprecationWarning) ret = grover.run(self._sv) self.assertTrue( Operator(ret['circuit']).equiv(Operator(self._expected))) def test_iterations(self): """Test the iterations argument""" grover = Grover(oracle=self._oracle, good_state=['111'], iterations=2) ret = grover.run(self._sv) self.assertTrue( Operator(ret['circuit']).equiv(Operator(self._expected))) grover = Grover(oracle=self._oracle, good_state=['111'], iterations=[1, 2, 3]) ret = grover.run(self._sv) self.assertTrue(ret.oracle_evaluation) self.assertIn(ret.top_measurement, ['111'])
class TestGroverFunctionality(QiskitAlgorithmsTestCase): """Test for the functionality of Grover""" def setUp(self): super().setUp() self._oracle = Statevector.from_label('111') self._expected_grover_op = GroverOperator(oracle=self._oracle) self._expected = QuantumCircuit(self._expected_grover_op.num_qubits) self._expected.compose(self._expected_grover_op.state_preparation, inplace=True) self._expected.compose(self._expected_grover_op.power(2), inplace=True) backend = BasicAer.get_backend('statevector_simulator') self._sv = QuantumInstance(backend) def test_iterations(self): """Test the iterations argument""" grover = Grover(oracle=self._oracle, good_state=['111'], iterations=2) ret = grover.run(self._sv) self.assertTrue(Operator(ret.circuit).equiv(Operator(self._expected))) grover = Grover(oracle=self._oracle, good_state=['111'], iterations=[1, 2, 3]) ret = grover.run(self._sv) self.assertTrue(ret.oracle_evaluation) self.assertIn(ret.top_measurement, ['111'])