You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+170Lines changed: 170 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -304,3 +304,173 @@ o (at index 29 [11101])
304
304
l (at index 21 [10101])
305
305
! (at index 10 [01010])
306
306
```
307
+
308
+
## Other Examples
309
+
310
+
Included in this project are other examples that demonstrate how to create an oracle for an implementation of Grover's algorithm.
311
+
312
+
These additional examples take advantage of the idea of using a controlled Z-Gate for setting the correct phase for desired measured values using Grover's algorithm. The key is to apply the Z-Gate to the qubits when the state of each qubit's value is the target.
313
+
314
+
## Using the Z-Gate in an Oracle for Grover's Algorithm
315
+
316
+
One of the easiest ways to construct an oracle for Grover's algorithm is to simply flip the phase of a single amplitude of the quantum circuit. This sets the detection for Grover's algorithm to identify the target result.
317
+
318
+
This can be done by applying a controlled Z-Gate across each qubit in the circuit. It doesn't matter which qubit ends up being the target versus control for the Z-Gate, so long as all qubits are included in the Z-Gate process.
319
+
320
+
For example, to instruct Grover's algorithm to find the state `1111`, we could use the following oracle shown below.
321
+
322
+
```
323
+
q_0: ──■─────
324
+
│
325
+
q_1: ──■─────
326
+
│
327
+
q_2: ──■─────
328
+
│
329
+
q_3: ──■─────
330
+
```
331
+
332
+
Similarly, to find the state `1011`, we can insert X-Gate (not gates) into our circuit. Since `HXH=Z` and `HZH=X`, we can take advantage of the quantum rules to create a multi-controlled phase circuit from a series of X-gates.
333
+
334
+
An example of finding the state `1011` can be constructed with the following code below.
335
+
336
+
```python
337
+
# Create a quantum circuit with one addition qubit for output.
338
+
qc = QuantumCircuit(5)
339
+
# Flip qubit 2 to detect a 0.
340
+
qc.x(2)
341
+
# Apply a controlled Z-Gate across each of the qubits. The target is simply the last qubit (although it does not matter which qubit is the target).
342
+
qc.append(ZGate().control(n), range(n+1))
343
+
# Unflip qubit 2 to restore the circuit.
344
+
qc.x(2)
345
+
```
346
+
347
+
The above code results in the following oracle.
348
+
349
+
```
350
+
q_0: ──────■──────
351
+
│
352
+
q_1: ──────■──────
353
+
┌───┐ │ ┌───┐
354
+
q_2: ┤ X ├─■─┤ X ├
355
+
└───┘ │ └───┘
356
+
q_3: ──────■──────
357
+
│
358
+
q_4: ──────■──────
359
+
```
360
+
361
+
Notice how we've applied an X-Gate around the Z-Gate control for qubit 2 (*note, we count qubits from right-to-left using Qiskit standard format*).
362
+
363
+
Running Grover's algorithm with the above oracle results in the following output.
Notice the number of occurrences for our target value `1011` has the highest count of `264`. Let's see howto apply this concept for actual applications, including detecting odd numbers, even numbers, and specific numeric values!
370
+
371
+
## Odd Numbers
372
+
373
+
The example for [odd numbers](odd.py) demonstrates a simple example of creating an oracle that finds all odd numbers in a given range of qubits. For example, when considering 3 qubits, we can create `2^3=8` different values. This includes the numbers 0-7, as shown below in binary form from each qubit.
374
+
375
+
**3 qubits**
376
+
377
+
```
378
+
000 = 0
379
+
001 = 1
380
+
010 = 2
381
+
011 = 3
382
+
100 = 4
383
+
101 = 5
384
+
110 = 6
385
+
111 = 7
386
+
```
387
+
388
+
We can see that the odd numbers all contain a `1` for the right-most digit (in binary). Therefore, we can create an oracle for Grover's algorithm to find all measurements of qubits that result in a `1` for the right-most digit by simply applying a controlled Z-Gate from the right-most qubit to all other qubits.
389
+
390
+
The oracle to find odd numbers is shown below.
391
+
392
+
q_0: ─■──■──■─
393
+
│ │ │
394
+
q_1: ─■──┼──┼─
395
+
│ │
396
+
q_2: ────■──┼─
397
+
│
398
+
q_3: ───────■─
399
+
400
+
Notice, we've used a controlled Z-Gate from qubit 0 to each of the other qubits. When qubit 0 has a value of 1, the Z-Gate is applied to each of the other qubits, setting the matching phase for Grover's algorithm.
401
+
402
+
The complete circuit is shown below.
403
+
404
+
```
405
+
┌───┐ ░ ┌─────────┐ ░ ┌───────────┐ ░ ┌─┐
406
+
var_0: ┤ H ├──────░─┤0 ├─░─┤0 ├─░──────┤M├───────────
407
+
├───┤ ░ │ │ ░ │ │ ░ └╥┘┌─┐
408
+
var_1: ┤ H ├──────░─┤1 ├─░─┤1 diffuser ├─░───────╫─┤M├────────
409
+
├───┤ ░ │ oracle │ ░ │ │ ░ ║ └╥┘┌─┐
410
+
var_2: ┤ H ├──────░─┤2 ├─░─┤2 ├─░───────╫──╫─┤M├─────
The above result also shows measurements for all possible odd numbers within a range of 5 qubits.
435
+
436
+
## Even Numbers
437
+
438
+
Similar to the example of odd numbers, we can apply the same process to create an oracle for measuring even numbers with Grover's algorithm.
439
+
440
+
Whereas with odd numbers we simply applied a Z-Gate from qubit 0 to all other qubits in order to set the measurement phase for Grover's algorithm when qubit 0 has a state of 1, this time we want to detect when qubit 0 has a state of 0.
441
+
442
+
That is, for 3-digit binary even numbers, we want to detect all values where the right-most bit is a 0.
443
+
444
+
We can do this by using the same controlled Z-Gate from qubit 0 to each of the other qubits. However, instead of measuring for a value of 1 on that qubit, we want to measure for a value of 0. To do this, we can simply flip the qubit before applying the controlled Z-Gate.
445
+
446
+
Note, we also have to "unflip" the first qubit back to its original state, in order to preserve the circuit for Grover's algorithm. The oracle is shown below.
447
+
448
+
```
449
+
┌───┐ ┌───┐
450
+
q_0: ┤ X ├─■──■──■─┤ X ├
451
+
└───┘ │ │ │ └───┘
452
+
q_1: ──────■──┼──┼──────
453
+
│ │
454
+
q_2: ─────────■──┼──────
455
+
│
456
+
q_3: ────────────■──────
457
+
```
458
+
459
+
The above oracle for measuring even numbers is nearly the exact same as that for measuring odd numbers, with the difference being the X-Gate applied to the first qubit.
460
+
461
+
### Output
462
+
463
+
The result of measuring for odd numbers results in the following output below.
0 commit comments