Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidmerwin/208629122f4970838b6d4bfa05a53380 to your computer and use it in GitHub Desktop.
Save davidmerwin/208629122f4970838b6d4bfa05a53380 to your computer and use it in GitHub Desktop.

Exercise 1: Two Same - Qubo Sampling and Results

Preview:
'''
Exercise 1
To run the exercise 1 demo from the command-line, type the command:
python two_same.py
Read through the code and take a look at the structure of the program. Notice the basic parts:
- Obtain a sampler/solver
- Define the Q matrix
- Run the problem, using the sampler/solver
- Print the results
In this exercise, we want to penalize the two qubit solutions in which the qubits have different values;
we want to favor the two qubit solutions in which the qubits have the same values.In the results,
'''
# Exercise 1: Two Same

# Import necessary libraries
from dwave.system import DWaveSampler, EmbeddingComposite
import dimod

# Define the Q matrix
Q = {(0, 0): -1, (1, 1): -1, (0, 1): 2}

# Obtain a sampler/solver
sampler = EmbeddingComposite(DWaveSampler())

# Run the problem using the sampler/solver
response = sampler.sample_qubo(Q, num_reads=10)

# Print the results
for sample, energy in response.data(['sample', 'energy']):
    print("Sample:", sample)
    print("Energy:", energy)
Associated Context
Type Code Snippet ( .py )
Associated Tags response Q matrix Sample Qubo Python Programming Framework: D-Wave Ocean SDK sample_qubo DWaveSampler num_reads dimod Q Matrix face-recognition EmbeddingComposite Num Readings numpy yolo computer-vision DWAVE Library Data Printing sampler Scientific Analysis Sampler/Solver sample energy
💡 Smart Description This code runs a problem using the DWaveSampler library, then prints each sample and energy of an array.
This code snippet solves a QUBO problem defined by the Q matrix using a D-Wave sampler. It prints the results, including the sample and energy for each solution found.
🔎 Suggested Searches DWaveSampler EmbeddingComposite example
How to use DWaveSampler in dwave library
Sample qubo using DWaveSampler and EmbeddingComposite
Example of embedding with DWaveSampler for sample_qubo.
How to solve a QUBO problem using DWaveSampler in Python?
Code example for solving a QUBO problem with DWaveSampler in Python
Python code for running a QUBO problem on DWaveSampler
Using EmbeddingComposite with DWaveSampler to solve a QUBO problem
Sample code for solving a QUBO problem with DWaveSampler and EmbeddingComposite
Related Links https://docs.ocean.dwavesys.com/en/latest/
https://www.w3schools.com/python/default.asp
https://www.w3schools.com/python/
https://www.w3schools.com/python/python_strings.asp
Related People David Merwin
Sensitive Information No Sensitive Information Detected
Shareable Link https://davidmerwin.pieces.cloud/?p=a56b44ab08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment