def test_converter_list(self): """Test converters list""" # Input. model = Model() x_0 = model.binary_var(name='x0') x_1 = model.binary_var(name='x1') model.maximize(-x_0+2*x_1) op = QuadraticProgram() op.from_docplex(model) # Get the optimum key and value. n_iter = 8 # a single converter. qp2qubo = QuadraticProgramToQubo() gmf = GroverOptimizer(4, num_iterations=n_iter, quantum_instance=self.sv_simulator, converters=qp2qubo) results = gmf.solve(op) self.validate_results(op, results) # a list of converters ineq2eq = InequalityToEquality() int2bin = IntegerToBinary() penalize = LinearEqualityToPenalty() converters = [ineq2eq, int2bin, penalize] gmf = GroverOptimizer(4, num_iterations=n_iter, quantum_instance=self.sv_simulator, converters=converters) results = gmf.solve(op) self.validate_results(op, results) # invalid converters with self.assertRaises(TypeError): invalid = [qp2qubo, "invalid converter"] GroverOptimizer(4, num_iterations=n_iter, quantum_instance=self.sv_simulator, converters=invalid)
def solve(self): # self.write_model() # IBMQ.enable_account(os.getenv('IBM_TOKEN')) backend = BasicAer.get_backend('ibmq_qasm_simulator') # backend = Aer.get_backend('qasm_simulator') prog = QuadraticProgram("molecConform") prog.read_from_lp_file(str(self.LP_FILEPATH)) optimizer = GroverOptimizer(3, quantum_instance=backend) results = optimizer.solve(prog) print(results) import pdb pdb.set_trace()
def test_qubo_gas_int_simple_maximize(self): """Test for simple case, but with maximization.""" # Input. model = Model() x_0 = model.binary_var(name='x0') x_1 = model.binary_var(name='x1') model.maximize(-x_0+2*x_1) op = QuadraticProgram() op.from_docplex(model) # Get the optimum key and value. n_iter = 8 gmf = GroverOptimizer(4, num_iterations=n_iter, quantum_instance=self.sv_simulator) results = gmf.solve(op) self.validate_results(op, results)
def test_qubo_gas_int_zero(self): """Test for when the answer is zero.""" # Input. model = Model() x_0 = model.binary_var(name='x0') x_1 = model.binary_var(name='x1') model.minimize(0*x_0+0*x_1) op = QuadraticProgram() op.from_docplex(model) # Will not find a negative, should return 0. gmf = GroverOptimizer(1, num_iterations=1, quantum_instance=self.q_instance) results = gmf.solve(op) self.assertEqual(results.x, [0, 0]) self.assertEqual(results.fval, 0.0)
def test_qubo_gas_int_paper_example(self): """Test the example from https://arxiv.org/abs/1912.04088.""" # Input. model = Model() x_0 = model.binary_var(name='x0') x_1 = model.binary_var(name='x1') x_2 = model.binary_var(name='x2') model.minimize(-x_0+2*x_1-3*x_2-2*x_0*x_2-1*x_1*x_2) op = QuadraticProgram() op.from_docplex(model) # Get the optimum key and value. n_iter = 10 gmf = GroverOptimizer(6, num_iterations=n_iter, quantum_instance=self.q_instance) results = gmf.solve(op) self.validate_results(op, results)
def test_qubo_gas_int_simple(self): """Test for simple case, with 2 linear coeffs and no quadratic coeffs or constants.""" # Input. model = Model() x_0 = model.binary_var(name='x0') x_1 = model.binary_var(name='x1') model.minimize(-x_0+2*x_1) op = QuadraticProgram() op.from_docplex(model) # Get the optimum key and value. n_iter = 8 gmf = GroverOptimizer(4, num_iterations=n_iter, quantum_instance=self.sv_simulator) results = gmf.solve(op) self.validate_results(op, results) self.assertIsNotNone(results.operation_counts) self.assertEqual(results.n_input_qubits, 2) self.assertEqual(results.n_output_qubits, 4)
backend = BasicAer.get_backend("statevector_simulator") model = Model() x0 = model.binary_var(name="x0") x1 = model.binary_var(name="x1") x2 = model.binary_var(name="x2") model.minimize(-x0 + 2 * x1 - 3 * x2 - 2 * x0 * x2 - 1 * x1 * x2) qp = QuadraticProgram() qp.from_docplex(model) print(qp.export_as_lp_string()) from qiskit.aqua import QuantumInstance grover_optimizer = GroverOptimizer(6, num_iterations=10, quantum_instance=backend) results = grover_optimizer.solve(qp) print("x={}".format(results.x)) print("fval={}".format(results.fval)) exact_solver = MinimumEigenOptimizer(NumPyMinimumEigensolver()) exact_result = exact_solver.solve(qp) print("x={}".format(exact_result.x)) print("fval={}".format(exact_result.fval)) # Since the `pytket` extension modules provide an interface to the widest variety of devices and simulators out of all major quantum software platforms, the simplest advantage to obtain through `pytket` is to try using some alternative backends. # # One such backend that becomes available is the Qulacs simulator, providing fast noiseless simulations, especially when exploiting an available GPU. We can wrap up the `QulacsBackend` (or `QulacsGPUBackend` if you have a GPU available) in a form that can be passed to any Qiskit Aqua algorithm. from pytket.extensions.qulacs import QulacsBackend from pytket.extensions.qiskit.tket_backend import TketBackend
quantumBackend = QuantumInstance(provider.get_backend('ibmq_qasm_simulator'), shots=2048, skip_qobj_validation=False) qaoa_mes = QAOA(quantum_instance=quantumBackend) qaoa = MinimumEigenOptimizer(qaoa_mes) # using QAOA qaoa_result = qaoa.solve(qubo) print(qaoa_result) exact_mes = NumPyMinimumEigensolver() exact = MinimumEigenOptimizer(exact_mes) # using the exact classical numpy minimum eigen solver exact_result = exact.solve(qubo) print(exact_result) rqaoa = RecursiveMinimumEigenOptimizer( min_eigen_optimizer=qaoa, min_num_vars=14, min_num_vars_optimizer=MinimumEigenOptimizer(NumPyMinimumEigensolver())) rqaoa_result = rqaoa.solve(qubo) print(rqaoa_result) grover_mes = GroverOptimizer(numOfItems + backPackCap, num_iterations=15, quantum_instance=quantumBackend) grover_result = grover_mes.solve(qubo) print(grover_result) print("x={}".format(grover_result.x)) print("fval={}".format(grover_result.fval))