def test_optimizationproblem_to_ising(self): """ Test optimization problem to operators""" op = QuadraticProgram() for i in range(4): op.binary_var(name='x{}'.format(i)) linear = {} for x in op.variables: linear[x.name] = 1 op.maximize(0, linear, {}) linear = {} for i, x in enumerate(op.variables): linear[x.name] = i + 1 op.linear_constraint(linear, Constraint.Sense.EQ, 3, 'sum1') penalize = LinearEqualityToPenalty() op2ope = QuadraticProgramToIsing() op2 = penalize.encode(op) qubitop, offset = op2ope.encode(op2) # the encoder uses a dictionary, in which the order of items in Python 3.5 is not # maintained, therefore don't do a list compare but dictionary compare qubit_op_as_dict = dict(qubitop.paulis) for coeff, paulis in QUBIT_OP_MAXIMIZE_SAMPLE.paulis: self.assertEqual(paulis, qubit_op_as_dict[coeff]) self.assertEqual(offset, OFFSET_MAXIMIZE_SAMPLE)
def test_optimizationproblem_to_ising_deprecated(self): """ Test optimization problem to operators""" op = QuadraticProgram() for i in range(4): op.binary_var(name='x{}'.format(i)) linear = {} for x in op.variables: linear[x.name] = 1 op.maximize(0, linear, {}) linear = {} for i, x in enumerate(op.variables): linear[x.name] = i + 1 op.linear_constraint(linear, Constraint.Sense.EQ, 3, 'sum1') penalize = LinearEqualityToPenalty(penalty=1e5) try: warnings.filterwarnings(action="ignore", category=DeprecationWarning) op2 = penalize.encode(op) conv = QuadraticProgramToIsing() qubitop, offset = conv.encode(op2) finally: warnings.filterwarnings(action="always", category=DeprecationWarning) self.assertEqual(qubitop, QUBIT_OP_MAXIMIZE_SAMPLE) self.assertEqual(offset, OFFSET_MAXIMIZE_SAMPLE)
def test_valid_variable_type_deprecated(self): """Validate the types of the variables for QuadraticProgramToIsing.""" # Integer variable try: warnings.filterwarnings(action="ignore", category=DeprecationWarning) conv = QuadraticProgramToIsing() finally: warnings.filterwarnings(action="always", category=DeprecationWarning) with self.assertRaises(QiskitOptimizationError): op = QuadraticProgram() op.integer_var(0, 10, "int_var") _ = conv.encode(op) # Continuous variable with self.assertRaises(QiskitOptimizationError): op = QuadraticProgram() op.continuous_var(0, 10, "continuous_var") _ = conv.encode(op)
def test_optimizationproblem_to_ising(self): """ Test optimization problem to operators""" op = QuadraticProgram() for i in range(4): op.binary_var(name='x{}'.format(i)) linear = {} for x in op.variables: linear[x.name] = 1 op.maximize(0, linear, {}) linear = {} for i, x in enumerate(op.variables): linear[x.name] = i + 1 op.linear_constraint(linear, Constraint.Sense.EQ, 3, 'sum1') penalize = LinearEqualityToPenalty() op2ope = QuadraticProgramToIsing() op2 = penalize.encode(op) qubitop, offset = op2ope.encode(op2) self.assertListEqual(qubitop.paulis, QUBIT_OP_MAXIMIZE_SAMPLE.paulis) self.assertEqual(offset, OFFSET_MAXIMIZE_SAMPLE)
def test_empty_problem(self): """ Test empty problem """ op = QuadraticProgram() conv = InequalityToEquality() op = conv.encode(op) conv = IntegerToBinary() op = conv.encode(op) conv = LinearEqualityToPenalty() op = conv.encode(op) conv = QuadraticProgramToIsing() _, shift = conv.encode(op) self.assertEqual(shift, 0.0)
def test_empty_problem_deprecated(self): """ Test empty problem """ try: warnings.filterwarnings(action="ignore", category=DeprecationWarning) op = QuadraticProgram() conv = InequalityToEquality() op = conv.encode(op) conv = IntegerToBinary() op = conv.encode(op) conv = LinearEqualityToPenalty() op = conv.encode(op) conv = QuadraticProgramToIsing() _, shift = conv.encode(op) finally: warnings.filterwarnings(action="always", category=DeprecationWarning) self.assertEqual(shift, 0.0)
def test_valid_variable_type(self): """Validate the types of the variables for QuadraticProgramToIsing.""" # Integer variable with self.assertRaises(QiskitOptimizationError): op = QuadraticProgram() op.integer_var(0, 10, "int_var") conv = QuadraticProgramToIsing() _ = conv.encode(op) # Continuous variable with self.assertRaises(QiskitOptimizationError): op = QuadraticProgram() op.continuous_var(0, 10, "continuous_var") conv = QuadraticProgramToIsing() _ = conv.encode(op)
# qc.cx(c[0], c[1]) #Acting the gates on circuit for i in range (g): gates() # Prepare the Ising Hamiltonian #n = 3 #number of qubits a = 1.0 k = 2 t = range(1, n+1) mdl = Model()# build model with docplex x = [mdl.binary_var() for i in range(n)] objective = a*(k - mdl.sum(t[i]*x[i] for i in range(n)))**2 mdl.minimize(objective) qp = QuadraticProgram()# convert to Qiskit's quadratic program qp.from_docplex(mdl) qp2ising = QuadraticProgramToIsing()# convert to Ising Hamiltonian H, offset = qp2ising.encode(qp) H_matrix = np.real(H.to_matrix()) psi = StateFn(qc) expectation_value = (~psi @ H @ psi).eval() print(psi) print(expectation_value)
# create a QUBO qubo = QuadraticProgram() qubo.binary_var('prof') qubo.binary_var('student') qubo.binary_var('cat') qubo.minimize(linear=[10, 5, 0], quadratic={ ('prof', 'student'): 4, ('prof', 'cat'): 2, ('student', 'cat'): -12 }) print(qubo.export_as_lp_string()) # In[7]: qp2op = QuadraticProgramToIsing() op, offset = qp2op.encode(qubo) print('offset: {}'.format(offset)) print('operator:') print(op.print_details()) # In[8]: from qiskit.circuit.library import RealAmplitudes from qiskit.aqua.components.optimizers import COBYLA qaoa_mes = QAOA(quantum_instance=BasicAer.get_backend('statevector_simulator')) vqe_mes = VQE( quantum_instance=BasicAer.get_backend('statevector_simulator'), var_form=RealAmplitudes(3, reps=2), # parametrized circuit to use