Skip to content

Commit c95a394

Browse files
Updated readme.
1 parent 625c09e commit c95a394

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

README.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,173 @@ o (at index 29 [11101])
304304
l (at index 21 [10101])
305305
! (at index 10 [01010])
306306
```
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.
364+
365+
```
366+
{'1101': 48, '0001': 47, '1100': 52, '0101': 40, '0100': 49, '0110': 46, '1110': 56, '0010': 63, '1000': 61, '1011': 264, '1111': 46, '1010': 54, '1001': 51, '0000': 49, '0111': 58, '0011': 40}
367+
```
368+
369+
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├─────
411+
├───┤┌───┐ ░ │ │ ░ └───────────┘ ░ ┌───┐ ║ ║ └╥┘┌───┐
412+
out_0: ┤ X ├┤ H ├─░─┤3 ├─░───────────────░─┤ H ├─╫──╫──╫─┤ X ├
413+
└───┘└───┘ ░ └─────────┘ ░ ░ └───┘ ║ ║ ║ └───┘
414+
c: 3/═════════════════════════════════════════════════╩══╩══╩══════
415+
0 1 2
416+
```
417+
418+
### Output
419+
420+
The result of applying Grover's algorithm with the odd numbers oracle is shown below.
421+
422+
```
423+
{'111': 254, '001': 232, '101': 275, '011': 263}
424+
```
425+
426+
The above result shows the measurements spread equally across each of the odd numbers within the range of 3 qubits.
427+
428+
We can likewise extrapolate the result out to 5 qubits, resulting in the following output below.
429+
430+
```
431+
{'01101': 68, '01001': 48, '10101': 69, '10111': 47, '00001': 77, '00101': 71, '01011': 68, '10011': 56, '01111': 65, '10001': 72, '11011': 71, '00111': 55, '00011': 66, '11101': 63, '11001': 61, '11111': 67}
432+
```
433+
434+
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.
464+
465+
```
466+
{'100': 242, '000': 271, '010': 260, '110': 251}
467+
```
468+
469+
## License
470+
471+
MIT
472+
473+
## Author
474+
475+
Kory Becker
476+
http://www.primaryobjects.com/kory-becker

0 commit comments

Comments
 (0)