def test_problem_large(self): problem = Problem(name="test", terms=[], problem_type=ProblemType.pubo) self.assertTrue(not problem.is_large()) problem.add_term(5.0, []) self.assertTrue(not problem.is_large()) problem.add_term(6.0, list(range(3000))) self.assertTrue(not problem.is_large()) problem.add_terms( [Term(indices=[9999], c=1.0)] * int(1e6) ) # create 1mil dummy terms self.assertTrue(problem.is_large())
def test_add_terms_cterms(self): problem = Problem(name="test") count = 4 for i in range(count): problem.add_term(c=i, indices=[i, i+1]) self.assertEqual(ProblemType.ising, problem.problem_type) self.assertEqual(count, len(problem.terms)) self.assertEqual(Term(c=1, indices=[1, 2]), problem.terms[1]) more = [] for i in range(count + 1): more.append(Term(c=i, indices=[i, i-1])) problem.add_terms(more) self.assertEqual((count * 2) + 1, len(problem.terms)) self.assertEqual(Term(c=count, indices=[count, count - 1]), problem.terms[count * 2])
def test_add_terms_cterms(self): problem = Problem(name="test") count = 4 for i in range(count): problem.add_term(c=i, indices=[i, i + 1]) self.assertEqual(ProblemType.ising, problem.problem_type) self.assertEqual(count, len(problem.terms)) self.assertEqual(Term(c=1, indices=[1, 2]), problem.terms[1]) more = [] for i in range(1, count + 1): more.append(Term(c=i, indices=[i - 1, i])) problem.add_terms(more) self.assertEqual(2 * count, len(problem.terms)) self.assertEqual(Term(c=count, indices=[count - 1, count]), problem.terms[-1]) subterms = [Term(c=1, indices=[i]) for i in range(count)] subterms.append(Term(c=-5, indices=[])) problem.add_slc_term(terms=[(1, i) for i in range(count)] + [(-5, None)], c=2) self.assertEqual(2 * count, len(problem.terms)) self.assertEqual(1, len(problem.terms_slc)) self.assertEqual(SlcTerm(subterms, c=2), problem.terms_slc[-1]) problem.add_terms(subterms, term_type=GroupType.squared_linear_combination, c=2) self.assertEqual(2 * count, len(problem.terms)) self.assertEqual(2, len(problem.terms_slc)) self.assertEqual(SlcTerm(subterms, c=2), problem.terms_slc[-1]) problem.add_terms(subterms, term_type=GroupType.combination, c=0.5) self.assertEqual(3 * count + 1, len(problem.terms)) self.assertEqual( [Term(subterm.ids, c=subterm.c) for subterm in subterms], problem.terms[-len(subterms):]) problem.add_slc_term(subterms, c=3) self.assertEqual(3 * count + 1, len(problem.terms)) self.assertEqual(3, len(problem.terms_slc)) self.assertEqual(SlcTerm(subterms, c=3), problem.terms_slc[-1])
# This allows you to connect to the Workspace you've previously deployed in Azure. # Be sure to fill in the settings below which can be retrieved by running 'az quantum workspace show' in the terminal. from azure.quantum import Workspace # Copy the settings for your workspace below workspace = Workspace(subscription_id="", resource_group="", name="", location="") # Define the problem problem = Problem(name="My First Problem", problem_type=ProblemType.ising) terms = [ Term(c=-9, indices=[0]), Term(c=-3, indices=[1, 0]), Term(c=5, indices=[2, 0]), Term(c=9, indices=[2, 1]), Term(c=2, indices=[3, 0]), Term(c=-4, indices=[3, 1]), Term(c=4, indices=[3, 2]) ] problem.add_terms(terms=terms) # Create the solver solver = ParallelTempering(workspace, timeout=100) # Solve the problem result = solver.optimize(problem) print(result)
# Workspace information workspace = Workspace( resource_id="", # add the Resource ID of your Azure Quantum workspace location= "" # add the location of your Azure Quantum workspace (e.g. "westus") ) # Define the problem problem = Problem(name="My First 1QBit Problem", problem_type=ProblemType.ising) problem.add_terms([ Term(w=-9, indices=[0]), Term(w=-3, indices=[1, 0]), Term(w=5, indices=[2, 0]), Term(w=9, indices=[2, 1]), Term(w=2, indices=[3, 0]), Term(w=-4, indices=[3, 1]), Term(w=4, indices=[3, 2]) ]) # Create 1QBit solvers print('instantiate solvers...') solvers = [ TabuSearch(workspace, improvement_cutoff=10), PticmSolver(workspace, num_sweeps_per_run=99), PathRelinkingSolver(workspace, distance_scale=0.44), ] # Submit the problem to each solver print('submit jobs...')