Ejemplo n.º 1
0
 def test_iterations_with_good_state(self, iterations):
     """Test the algorithm with different iteration types and with good state"""
     grover = Grover(iterations, quantum_instance=self.statevector)
     problem = AmplificationProblem(Statevector.from_label("111"),
                                    is_good_state=["111"])
     result = grover.amplify(problem)
     self.assertEqual(result.top_measurement, "111")
Ejemplo n.º 2
0
 def test_max_power(self):
     """Test the iteration stops when the maximum power is reached."""
     lam = 10.0
     grover = Grover(growth_rate=lam, quantum_instance=self.statevector)
     problem = AmplificationProblem(Statevector.from_label("111"), is_good_state=["111"])
     result = grover.amplify(problem)
     self.assertEqual(len(result.iterations), 0)
Ejemplo n.º 3
0
 def test_implicit_phase_oracle_is_good_state(self):
     """Test implicit default for is_good_state with PhaseOracle."""
     grover = Grover(iterations=2, quantum_instance=self.statevector)
     oracle = PhaseOracle("x | x")
     problem = AmplificationProblem(oracle)
     result = grover.amplify(problem)
     self.assertEqual(result.top_measurement, "0")
Ejemplo n.º 4
0
 def test_multiple_iterations(self):
     """Test the algorithm for a list of iterations."""
     grover = Grover(iterations=[1, 2, 3],
                     quantum_instance=self.statevector)
     problem = AmplificationProblem(Statevector.from_label('111'),
                                    is_good_state=['111'])
     result = grover.amplify(problem)
     self.assertEqual(result.top_measurement, '111')
Ejemplo n.º 5
0
 def test_max_probability(self):
     """Test max_probability"""
     oracle = QuantumCircuit(2)
     oracle.cz(0, 1)
     problem = AmplificationProblem(oracle, is_good_state=["11"])
     grover = Grover(quantum_instance=self.qasm)
     result = grover.amplify(problem)
     self.assertEqual(result.max_probability, 1.0)
Ejemplo n.º 6
0
 def test_oracle_evaluation(self):
     """Test oracle_evaluation for PhaseOracle"""
     oracle = PhaseOracle("x1 & x2 & (not x3)")
     problem = AmplificationProblem(oracle, is_good_state=oracle.evaluate_bitstring)
     grover = Grover(quantum_instance=self.qasm)
     result = grover.amplify(problem)
     self.assertTrue(result.oracle_evaluation)
     self.assertEqual("011", result.top_measurement)
Ejemplo n.º 7
0
    def test_run_state_vector_oracle(self):
        """Test execution with a state vector oracle"""
        mark_state = Statevector.from_label('11')
        problem = AmplificationProblem(mark_state, is_good_state=['11'])

        grover = Grover(quantum_instance=self.qasm)
        result = grover.amplify(problem)
        self.assertIn(result.top_measurement, ['11'])
Ejemplo n.º 8
0
    def test_run_circuit_oracle(self):
        """Test execution with a quantum circuit oracle"""
        oracle = QuantumCircuit(2)
        oracle.cz(0, 1)
        problem = AmplificationProblem(oracle, is_good_state=['11'])

        grover = Grover(quantum_instance=self.qasm)
        result = grover.amplify(problem)
        self.assertIn(result.top_measurement, ['11'])
Ejemplo n.º 9
0
 def test_run_circuit_oracle(self):
     """Test execution with a quantum circuit oracle"""
     oracle = QuantumCircuit(2)
     oracle.cz(0, 1)
     problem = AmplificationProblem(oracle, is_good_state=["11"])
     qi = QuantumInstance(self._provider.get_backend("fake_yorktown"),
                          seed_simulator=12,
                          seed_transpiler=32)
     grover = Grover(quantum_instance=qi)
     result = grover.amplify(problem)
     self.assertIn(result.top_measurement, ["11"])
Ejemplo n.º 10
0
 def test_run_circuit_oracle_single_experiment_backend(self):
     """Test execution with a quantum circuit oracle"""
     oracle = QuantumCircuit(2)
     oracle.cz(0, 1)
     problem = AmplificationProblem(oracle, is_good_state=["11"])
     backend = self._provider.get_backend("fake_vigo")
     backend._configuration.max_experiments = 1
     qi = QuantumInstance(backend, seed_simulator=12, seed_transpiler=32)
     grover = Grover(quantum_instance=qi)
     result = grover.amplify(problem)
     self.assertIn(result.top_measurement, ["11"])
Ejemplo n.º 11
0
    def test_max_num_iterations(self):
        """Test the iteration stops when the maximum number of iterations is reached."""
        def zero():
            while True:
                yield 0

        grover = Grover(iterations=zero(), quantum_instance=self.statevector)
        n = 5
        problem = AmplificationProblem(Statevector.from_label('1' * n),
                                       is_good_state=['1' * n])
        result = grover.amplify(problem)
        self.assertEqual(len(result.iterations), 2**n)
Ejemplo n.º 12
0
    def test_run_custom_grover_operator(self):
        """Test execution with a grover operator oracle"""
        oracle = QuantumCircuit(2)
        oracle.cz(0, 1)
        grover_op = GroverOperator(oracle)
        problem = AmplificationProblem(oracle=oracle,
                                       grover_operator=grover_op,
                                       is_good_state=['11'])

        grover = Grover(quantum_instance=self.qasm)
        ret = grover.amplify(problem)
        self.assertIn(ret.top_measurement, ['11'])
Ejemplo n.º 13
0
 def test_circuit_result(self):
     """Test circuit_result"""
     oracle = QuantumCircuit(2)
     oracle.cz(0, 1)
     # is_good_state=['00'] is intentionally selected to obtain a list of results
     problem = AmplificationProblem(oracle, is_good_state=["00"])
     grover = Grover(iterations=[1, 2, 3, 4], quantum_instance=self.qasm)
     result = grover.amplify(problem)
     expected_results = [
         {"11": 1024},
         {"00": 238, "01": 253, "10": 263, "11": 270},
         {"00": 238, "01": 253, "10": 263, "11": 270},
         {"11": 1024},
     ]
     self.assertEqual(result.circuit_results, expected_results)
Ejemplo n.º 14
0
    def test_iterator(self):
        """Test running the algorithm on an iterator."""

        # step-function iterator
        def iterator():
            wait, value, count = 3, 1, 0
            while True:
                yield value
                count += 1
                if count % wait == 0:
                    value += 1

        grover = Grover(iterations=iterator(), quantum_instance=self.statevector)
        problem = AmplificationProblem(Statevector.from_label("111"), is_good_state=["111"])
        result = grover.amplify(problem)
        self.assertEqual(result.top_measurement, "111")
Ejemplo n.º 15
0
 def test_growth_rate(self):
     """Test running the algorithm on a growth rate"""
     grover = Grover(growth_rate=8 / 7, quantum_instance=self.statevector)
     problem = AmplificationProblem(Statevector.from_label("111"), is_good_state=["111"])
     result = grover.amplify(problem)
     self.assertEqual(result.top_measurement, "111")
Ejemplo n.º 16
0
 def test_fixed_iterations_without_good_state(self):
     """Test the algorithm with iterations as an int and without good state"""
     grover = Grover(iterations=2, quantum_instance=self.statevector)
     problem = AmplificationProblem(Statevector.from_label("111"))
     result = grover.amplify(problem)
     self.assertEqual(result.top_measurement, "111")
 def test_fixed_iterations(self):
     """Test the iterations argument"""
     grover = Grover(iterations=2, quantum_instance=self.statevector)
     problem = AmplificationProblem(Statevector.from_label("111"), is_good_state=["111"])
     result = grover.amplify(problem)
     self.assertEqual(result.top_measurement, "111")