Our example uses a Bell state circuit to illustrate all the fundamental Qiskit basics.
Sample program output,
counts
are randomized each time.First we take the quantum state vector immediately after the input.We understand that the first element of
input:
state:
Statevector([1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
dims=(2, 2))
probs:
[1. 0. 0. 0.]
Statevector
is , and has probability of 1.0.Next we take the state after a Hadamard gate on the first qubit:We now understand that the second element of the
h:
state:
Statevector([0.70710678+0.j, 0.70710678+0.j, 0. +0.j,
0. +0.j],
dims=(2, 2))
probs:
[0.5 0.5 0. 0. ]
Statevector
is , and now we have a 50/50 propabability split for the first bit.Then we apply the CNOT gate:which leaves us with the final .
cx:
state:
Statevector([0.70710678+0.j, 0. +0.j, 0. +0.j,
0.70710678+0.j],
dims=(2, 2))
probs:
[0.5 0. 0. 0.5]
Then we print the circuit a bit:
qc without measure:
┌───┐
q_0: ┤ H ├──■──
└───┘┌─┴─┐
q_1: ─────┤ X ├
└───┘
c: 2/══════════
qc with measure:
┌───┐ ┌─┐
q_0: ┤ H ├──■──┤M├───
└───┘┌─┴─┐└╥┘┌─┐
q_1: ─────┤ X ├─╫─┤M├
└───┘ ║ └╥┘
c: 2/═══════════╩══╩═
0 1
qasm:
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0],q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];
And finally we compile the circuit and do some sample measurements:
qct:
┌───┐ ┌─┐
q_0: ┤ H ├──■──┤M├───
└───┘┌─┴─┐└╥┘┌─┐
q_1: ─────┤ X ├─╫─┤M├
└───┘ ║ └╥┘
c: 2/═══════════╩══╩═
0 1
counts={'11': 484, '00': 516}
counts={'11': 493, '00': 507}
This is an example of the
qiskit.circuit.library.QFT
implementation of the Quantum Fourier transform function which is documented at: docs.quantum.ibm.com/api/qiskit/0.44/qiskit.circuit.library.QFTOutput:So this also serves as a more interesting example of quantum compilation, mapping the
init: [1, 0, 0, 0, 0, 0, 0, 0]
qc
┌──────────────────────────────┐┌──────┐
q_0: ┤0 ├┤0 ├
│ ││ │
q_1: ┤1 Initialize(1,0,0,0,0,0,0,0) ├┤1 QFT ├
│ ││ │
q_2: ┤2 ├┤2 ├
└──────────────────────────────┘└──────┘
transpiled qc
┌──────────────────────────────┐ ┌───┐
q_0: ┤0 ├────────────────────■────────■───────┤ H ├─X─
│ │ ┌───┐ │ │P(π/2) └───┘ │
q_1: ┤1 Initialize(1,0,0,0,0,0,0,0) ├──────■───────┤ H ├─┼────────■─────────────┼─
│ │┌───┐ │P(π/2) └───┘ │P(π/4) │
q_2: ┤2 ├┤ H ├─■─────────────■──────────────────────X─
└──────────────────────────────┘└───┘
Statevector([0.35355339+0.j, 0.35355339+0.j, 0.35355339+0.j,
0.35355339+0.j, 0.35355339+0.j, 0.35355339+0.j,
0.35355339+0.j, 0.35355339+0.j],
dims=(2, 2, 2))
init: [0.0, 0.35355339059327373, 0.5, 0.3535533905932738, 6.123233995736766e-17, -0.35355339059327373, -0.5, -0.35355339059327384]
Statevector([ 7.71600526e-17+5.22650714e-17j,
1.86749130e-16+7.07106781e-01j,
-6.10667421e-18+6.10667421e-18j,
1.13711443e-16-1.11022302e-16j,
2.16489014e-17-8.96726857e-18j,
-5.68557215e-17-1.11022302e-16j,
-6.10667421e-18-4.94044770e-17j,
-3.30200457e-16-7.07106781e-01j],
dims=(2, 2, 2))
QFT
gate to Qiskit Aer primitives.If we don't
transpile
in this example, then running blows up with:qiskit_aer.aererror.AerError: 'unknown instruction: QFT'
The second input is:and the output of that approximately:which can be defined simply as the normalized DFT of the input quantum state vector.
[0, 1j/sqrt(2), 0, 0, 0, 0, 0, 1j/sqrt(2)]
From this we see that the Quantum Fourier transform is equivalent to a direct discrete Fourier transform on the quantum state vector, related: physics.stackexchange.com/questions/110073/how-to-derive-quantum-fourier-transform-from-discrete-fourier-transform-dft
This function does quantum compilation. Shown e.g. at qiskit/qft.py.
As en.wikipedia.org/w/index.php?title=ZX-calculus&oldid=1071329204#Diagram_rewriting tries to explain but fails to deliver as usual consider the GHZ state represented as a quantum circuit.
How can we easily prove that that quantum circuit equals the state:?
The naive way would be to just do the matrix multiplication as explained at Section "Quantum computing is just matrix multiplication".
However, ZX-calculus provides a simpler way.
And even more importantly, sometimes it is the only way, because in a real circuit, we would not be able to do the matrix multiplication
What we do in ZX-calculus is we first transform the original quantum circuit into a ZX graph.
This is always possible, because we can describe how to do the conversion simply for any of the Clifford plus T gates, which is a set of universal quantum gates.
Then, after we do this transformation, we can start applying further transformations that simplify the circuit.
It has already been proven that there is no efficient algorithm for this (TODO source, someone said P-sharp complete best case)
But it has been proven in 2017 that any possible equivalence between quantum circuits can be reached by modifying ZX-calculus circuits.
There are only 7 transformation rules that we need, and all others can be derived from those, universality.
So, we can apply those rules to do the transformation shown in Wikipedia:
and one of those rules finally tells us that that last graph means our desired state:because it is a Z spider with and .
Bibliography:
- quantumcomputing.stackexchange.com/questions/9774/what-are-some-applications-of-the-zx-calculus
- github.com/zxcalc/book Picturing Quantum Software by Aleks Kissinger and John van de Wetering (2024), CC BY-NC-SA.