-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem1
207 lines (151 loc) · 7.25 KB
/
Problem1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
## Projects Directory Structure
```bash
(BELIEF SPACE)
├── README.md
├── code
│ └── number_partitioning.py
├── data
│ └── training_number_partitioning.json
├── math_equations
│ └── equations.md
├── results
│ └── sampling_time_analysis.md
└── quantum_tsp
├── code
│ └── quantum_tsp.py
├── data
│ └── cities.json
├── math_equations
│ └── tsp_equations.md
└── results
└── tsp_results.md
├── project_cd24acdf-0a8f-44ae-a150-69ece39acec6
│ ├── code
│ ├── data
│ └── ...
└── project_6dbabf04-9a93-4534-9239-51d9edc7cbe2
├── code
├── data
└── … (expand here)
🔤 Title ====================================
Exercise 1: Two Same - Qubo Sampling and Results
📝 Description ==============================
This code runs a problem using the DWaveSampler library, then prints each sample and energy of an array.
'''
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)
### Table of Keywords and Concepts
| Keyword/Concept | Description | Python3 Example | Mathematical Equation |
|-----------------------|------------------------------------------------------------------------------------------------------------------------|-----------------------------|-------------------------------|
| QUBO (Quadratic Unconstrained Binary Optimization) | Mathematical representation for optimization problems that can be solved on D-Wave quantum computers | `Q = {(0, 0): -1, (1, 1): -1, (0, 1): 2}` | \(Q_{ij}x_i x_j\) |
| Sampler/Solver | Backend computational resource for solving QUBO problems | `sampler = EmbeddingComposite(DWaveSampler())` | N/A |
| Energy | Measurement of solution quality. Lower is better. | `"Energy:", energy` | \(E = \sum Q_{ij}x_i x_j\) |
| EmbeddingComposite | Handles the mapping between logical and physical qubits | `EmbeddingComposite(DWaveSampler())` | N/A |
| num_reads | Number of times the problem is solved to gather statistics | `num_reads=10` | N/A |
### Crash Course
#### Understanding QUBO
- QUBO is a way to represent optimization problems, especially for D-Wave's quantum annealing machines.
- The Q matrix is the mathematical representation of the QUBO problem.
```python
# Defining Q matrix in Python
Q = {(0, 0): -1, (1, 1): -1, (0, 1): 2}
```
- **Mathematical Equation**: \(Q = -x_0^2 - x_1^2 + 2x_0x_1\)
#### Sampler/Solver
- A sampler/solver is a computational resource you can use to solve the QUBO problem.
```python
# Obtain a sampler in Python
sampler = EmbeddingComposite(DWaveSampler())
```
#### Energy
- Energy is a measure of how optimal the solution is. The goal is to minimize this energy.
```python
# Printing energy in Python
print("Energy:", energy)
```
- **Mathematical Equation**: \(E = \sum Q_{ij}x_i x_j\)
### Study Guide
1. **Install Necessary Libraries**
- `pip install dwave-ocean-sdk`
2. **Understand QUBO**
- Study the concept and mathematical representation. Try creating simple Q matrices.
3. **Coding Practice**
- Get experience by running simple programs to solve QUBO problems.
4. **Energy Understanding**
- Dive deep into how energy correlates with the quality of the solutions.
5. **Consult Documentation and Community**
- Always refer to [D-Wave Documentation](https://docs.dwavesys.com/docs/latest/index.html) and forums.
6. **Loop Over `num_reads`**
- For statistical validation, run your problem multiple times.
```python
# Sample code integrating all the above points
from dwave.system import DWaveSampler, EmbeddingComposite
import dimod
Q = {(0, 0): -1, (1, 1): -1, (0, 1): 2} # Step 2
sampler = EmbeddingComposite(DWaveSampler()) # Step 3
response = sampler.sample_qubo(Q, num_reads=10) # Step 7
for sample, energy in response.data(['sample', 'energy']):
print("Sample:", sample)
print("Energy:", energy) # Step 4
```
### Markdown
```markdown
# Quantum Computing Training Project
## Exercise 1: Two Same Qubits
### Objective
In this exercise, we focus on penalizing two-qubit solutions in which the qubits have different values and favor solutions where qubits have the same values.
### Technologies Used
- D-Wave Quantum Annealer
- Python3
- QUBO
### Code Snippet
```python
from dwave.system import DWaveSampler, EmbeddingComposite
import dimod
Q = {(0, 0): -1, (1, 1): -1, (0, 1): 2}
sampler = EmbeddingComposite(DWaveSampler())
response = sampler.sample_qubo(Q, num_reads=10)
for sample, energy in response.data(['sample', 'energy']):
print("Sample:", sample)
print("Energy:", energy)
```
### Mathematical Concepts
- QUBO Matrix: \( Q = -x_0^2 - x_1^2 + 2x_0x_1 \)
- Energy: \( E = \sum Q_{ij}x_i x_j \)
### Results
The results provided the optimal solutions that minimize the energy, providing the ideal state of the qubits.
🏷️ 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
👥 People ===================================
David Merwin - davidmerwin1992@gmail.com
🔗 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
https://davidmerwin.pieces.cloud/?p=a56b44ab08
https://gist.github.com/208629122f4970838b6d4bfa05a53380
https://cloud.dwavesys.com/leap/