def test_same_embedding(self): sampler = LazyEmbeddingComposite(MockSampler()) # Set up Ising and sample h = {'a': 1, 'b': 1, 'c': 1} J = {('a', 'b'): 3, ('b', 'c'): -2, ('a', 'c'): 1} sampler.sample_ising(h, J) # Store embedding prev_embedding = sampler.embedding # Check that the same embedding is used csp2 = dbc.ConstraintSatisfactionProblem(dbc.BINARY) csp2.add_constraint(or_gate(['a', 'b', 'c'])) bqm2 = dbc.stitch(csp2) sampler.sample(bqm2) self.assertEqual(sampler.embedding, prev_embedding)
# -*- coding: utf-8 -*- # https://support.dwavesys.com/hc/en-us/community/posts/360016737274-Save-time-Reuse-your-embedding-when-possible import dwavebinarycsp as dbc import dwavebinarycsp.factories.constraint.gates as gates from dwave.system.composites import FixedEmbeddingComposite, EmbeddingComposite from dwave.system.samplers import DWaveSampler # Making two different BQMs (think: energy functions or optimization functions) csp1 = dbc.ConstraintSatisfactionProblem(dbc.BINARY) csp1.add_constraint(gates.and_gate(['a', 'b', 'c'])) bqm1 = dbc.stitch(csp1) csp2 = dbc.ConstraintSatisfactionProblem(dbc.BINARY) csp2.add_constraint(gates.or_gate(['a', 'b', 'c'])) bqm2 = dbc.stitch(csp2) # Using Embedding Composite sampler = EmbeddingComposite(DWaveSampler()) sampler.sample(bqm1) # Gets a new embedding for bqm1 sampler.sample(bqm2) # Gets a new embedding for bqm2 # Using Fixed Embedding Composite # Note: bqm1 and bqm2 can both be represented by the same graph - triangle graph. embedding = { 'a': [0, 4], 'b': [1], 'c': [5] } # Embedding the triangle graph using QPU indices fixedSampler = FixedEmbeddingComposite(DWaveSampler(), embedding)
not6 = not or4 or7 = and5 or not6 return(z == or7) csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY) csp.add_constraint(logic_circuit, ['a', 'b', 'c', 'd', 'z']) #Multiple Constraints import dwavebinarycsp import dwavebinarycsp.factories.constraint.gates as gates import operator csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY) csp.add_constraint(operator.ne, ['b', 'not1']) #add NOT 1 gate csp.add_constraint(gates.or_gate(['b', 'c', 'or2'])) #add OR 2 gate csp.add_constraint(gates.and_gate(['a', 'not1', 'and3'])) # add AND 3 gate csp.add_constraint(gates.or_gate(['d', 'or2', 'or4'])) # add OR 4 gate csp.add_constraint(gates.and_gate(['and3', 'or4', 'and5'])) # add AND 5 gate csp.add_constraint(operator.ne, ['or4', 'not6']) # add NOT 6 gate csp.add_constraint(gates.or_gate(['and5', 'not6', 'z']) # add OR 7 gate # Convert the binary constraint satisfaction problem to a binary quadratic model bqm = dwavebinarycsp.stitch(csp) --------- #The next code sets up the D-Wave sampler ---------
For the intermediate variables we will use: (xor1, xor2, and1, and2, or1) However, s and xor2 are the same, and cOut and or1 are the same. See this image for a graphical view of this circuit: https://upload.wikimedia.org/wikipedia/commons/5/57/Fulladder.gif """ csp.add_constraint(gates.xor_gate(['a', 'b', 'xor1'])) # xor(a,b) = xor1 csp.add_constraint(gates.xor_gate(['xor1', 'cIn', 's'])) # xor(xor1,cIn) = s csp.add_constraint(gates.and_gate(['xor1', 'cIn', 'and1'])) # and(xor1,cIn) = and1 csp.add_constraint(gates.and_gate(['a', 'b', 'and2'])) # and(a,b) = and2 csp.add_constraint(gates.or_gate(['and1', 'and2', 'cOut'])) # or(and1,and2) = cOut # This is an example of an assert I used to ensure my constraints above # faithfully reproduced the full-adder I want to implement. # https://docs.ocean.dwavesys.com/projects/binarycsp/en/latest/reference/generated/dwavebinarycsp.ConstraintSatisfactionProblem.check.html assert csp.check({ 'a': 1, 'and1': 0, 'and2': 1, 'b': 1, 'cIn': 0, 'cOut': 1, 's': 0, 'xor1': 0 })
## Rather than using a QPU gate-based solution as shown in the QCEngine version, ## this demo implements the same circuit using quantum annealing to find ## the answer, by linking it to the "energy level" of a system. ## The D-Wave website contqins an extensive set of tools, documentation, ## and learning materials to help you get started. ## Required imports import dwavebinarycsp import dwavebinarycsp.factories.constraint.gates as gates import operator from dimod.reference.samplers import ExactSolver ## Set up the logic gates, exactly as shown in Figure 10-7 of the book. csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY) csp.add_constraint(gates.or_gate(['boxA', 'boxB', 'AorB'])) # OR gate csp.add_constraint(operator.ne, ['boxA', 'notA']) # NOT gate csp.add_constraint(gates.xor_gate(['AorB', 'notA', 'notAxorAorB'])) # XOR gate csp.add_constraint(operator.ne, ['notAxorAorB', 'result']) # NOT gate csp.add_constraint(operator.eq, ['result', 'one']) # Specify that the result should be one csp.fix_variable( 'one', 1) # We could fix 'result' directly, but this is handy for printing bqm = dwavebinarycsp.stitch(csp) ## Run the solver sampler = ExactSolver() response = sampler.sample(bqm) ## Print all of the results for datum in response.data(['sample', 'energy']):
## The D-Wave website contqins an extensive set of tools, documentation, ## and learning materials to help you get started. ## Required imports import dwavebinarycsp import dwavebinarycsp.factories.constraint.gates as gates import operator from dimod.reference.samplers import ExactSolver ## Set up the logic gates, to implement this function: ## (a OR b) AND (NOT a OR c) AND (NOT b OR NOT c) AND (a OR c) csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY) csp.add_constraint(operator.ne, ['a', 'na']) # NOT gate csp.add_constraint(operator.ne, ['b', 'nb']) # NOT gate csp.add_constraint(operator.ne, ['c', 'nc']) # NOT gate csp.add_constraint(gates.or_gate(['a', 'b', 'a_or_b'])) # OR gate csp.add_constraint(gates.or_gate(['na', 'c', 'na_or_c'])) # OR gate csp.add_constraint(gates.or_gate(['nb', 'nc', 'nb_or_nc'])) # OR gate csp.add_constraint(gates.or_gate(['a', 'c', 'a_or_c'])) # OR gate csp.add_constraint(gates.and_gate(['a_or_b', 'na_or_c', 'and1'])) # AND gate csp.add_constraint(gates.and_gate(['nb_or_nc', 'a_or_c', 'and2'])) # AND gate csp.add_constraint(gates.and_gate(['and1', 'and2', 'result'])) # AND gate csp.fix_variable('result', 1) # Specify that the result should be one bqm = dwavebinarycsp.stitch(csp) ## Run the solver sampler = ExactSolver() response = sampler.sample(bqm) ## Print all of the results for datum in response.data(['sample', 'energy']):
## Rather than using a QPU gate-based solution as shown in the QCEngine version, ## this demo implements the same circuit using quantum annealing to find ## the answer, by linking it to the "energy level" of a system. ## In this example, we build the circuit shown in Figure 10-3 in the book, ## and then simply print out all of the energy values. ## Required imports import dwavebinarycsp import dwavebinarycsp.factories.constraint.gates as gates import operator from dimod.reference.samplers import ExactSolver ## Set up the logic gates, exactly as shown in Figure 10-3 in the book. csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY) csp.add_constraint(operator.ne, ['b', 'not_b']) csp.add_constraint(gates.or_gate(['a', 'not_b', 'a_or_not_b'])) csp.add_constraint(gates.and_gate(['c', 'a_or_not_b', 'result'])) bqm = dwavebinarycsp.stitch(csp) ## Run the solver sampler = ExactSolver() response = sampler.sample(bqm) ## Interpret the results print('All results: --------------------------------') for datum in response.data(['sample', 'energy']): print(datum.sample, datum.energy)