|
| 1 | +```@meta |
| 2 | +EditURL = "../../../example/toffoli.jl" |
| 3 | +``` |
| 4 | + |
| 5 | +# Toffoli Gate Example |
| 6 | +Here we define the Toffoli gate (CCNOT) using the GroverAlgorithm package |
| 7 | +This is an example of how to use the package. |
| 8 | +Toffoli gate is available in ITensorMPS.jl as a built-in gate. |
| 9 | +Let's create a Toffoli gate and check its action on the basis states. |
| 10 | + |
| 11 | +## Using the built-in Toffoli gate |
| 12 | +In ITensorMPS.jl, the Toffoli gate is available as a built-in three-qubit gate. We can simply add it to our quantum circuit and execute it. |
| 13 | + |
| 14 | +````@example toffoli |
| 15 | +using Printf |
| 16 | +using GroverAlgorithm |
| 17 | +using ITensors, ITensorMPS |
| 18 | +
|
| 19 | +circuit = QuantumCircuit(3) |
| 20 | +add_gate!(circuit, ThreeQubitGate(1, 2, 3, :Toffoli)) |
| 21 | +
|
| 22 | +sites = siteinds("Qubit", 3) |
| 23 | +psi = execute_circuit(circuit, sites) |
| 24 | +counts = measure(psi, Sampling(100)) |
| 25 | +println("結果: ", counts) |
| 26 | +```` |
| 27 | + |
| 28 | +Toffoli gate acts as follows for the basis states: |
| 29 | + |
| 30 | +````@example toffoli |
| 31 | +function test_actions(circuit) |
| 32 | + println("Test of Toffoli Gate") |
| 33 | + println("=" ^ 50) |
| 34 | + println("Input | Output") |
| 35 | + println("-" ^ 50) |
| 36 | + for a in 0:1, b in 0:1, c in 0:1 |
| 37 | + initial = AbstractInitialState[ProductState([string(a), string(b), string(c)])] |
| 38 | + set_state!(circuit, initial) |
| 39 | +
|
| 40 | + sites = siteinds("Qubit", 3) |
| 41 | + psi = execute_circuit(circuit, sites) |
| 42 | +
|
| 43 | + counts = measure(psi, Sampling(1)) |
| 44 | + output = collect(keys(counts))[1] |
| 45 | + @printf("|%d%d%d⟩ → |%s⟩\n", a, b, c, output) |
| 46 | + end |
| 47 | +end |
| 48 | +
|
| 49 | +test_actions(circuit) |
| 50 | +```` |
| 51 | + |
| 52 | +## Toffoli gate decomposition |
| 53 | +Then, Lets's construct the Toffoli gate using the standard decomposition into CNOT and single-qubit gates, |
| 54 | +and verify that it produces the same results as the built-in Toffoli gate. |
| 55 | + |
| 56 | +````@example toffoli |
| 57 | +function toffoli_decomposed(circuit) |
| 58 | + add_gate!(circuit, SingleQubitGate(3, :H)) |
| 59 | + add_gate!(circuit, ControlledGate(2, 3, :CX)) |
| 60 | + add_gate!(circuit, SingleQubitGate(3, :Tdag)) |
| 61 | + add_gate!(circuit, ControlledGate(1, 3, :CX)) |
| 62 | + add_gate!(circuit, SingleQubitGate(3, :T)) |
| 63 | + add_gate!(circuit, ControlledGate(2, 3, :CX)) |
| 64 | + add_gate!(circuit, SingleQubitGate(2, :Tdag)) |
| 65 | + add_gate!(circuit, SingleQubitGate(3, :Tdag)) |
| 66 | + add_gate!(circuit, ControlledGate(1, 3, :CX)) |
| 67 | + add_gate!(circuit, ControlledGate(1, 2, :CX)) |
| 68 | + add_gate!(circuit, SingleQubitGate(1, :T)) |
| 69 | + add_gate!(circuit, SingleQubitGate(2, :Tdag)) |
| 70 | + add_gate!(circuit, SingleQubitGate(3, :T)) |
| 71 | + add_gate!(circuit, ControlledGate(1, 2, :CX)) |
| 72 | + add_gate!(circuit, SingleQubitGate(2, :S)) |
| 73 | + add_gate!(circuit, SingleQubitGate(3, :H)) |
| 74 | + return circuit |
| 75 | +end |
| 76 | +circuit_decomposed = QuantumCircuit(3) |
| 77 | +toffoli_decomposed(circuit_decomposed) |
| 78 | +
|
| 79 | +tp = to_tikz_picture(circuit_decomposed) |
| 80 | +```` |
| 81 | + |
| 82 | +Let's test the action of the decomposed Toffoli gate on the basis states to verify that it behaves the same as the built-in Toffoli gate. |
| 83 | + |
| 84 | +````@example toffoli |
| 85 | +test_actions(circuit_decomposed) |
| 86 | +```` |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).* |
| 91 | + |
0 commit comments