In Qiskit at: qiskit/hello.py.
The CNOT gate is a controlled quantum gate that operates on two qubits, flipping the second (operand) qubit if the first (control) qubit is set.
This gate is the first example of a controlled quantum gate that you should study.
To understand why the gate is called a CNOT gate, you should think as follows.
First let's produce a generic quantum state vector where the control qubit is certain to be 0.
On the standard basis:we see that this means that only and should be possible. Therefore, the state must be of the form:where and are two complex numbers such that
If we operate the CNOT gate on that state, we obtain:and so the input is unchanged as desired, because the control qubit is 0.
If however we take only states where the control qubit is for sure 1:
Therefore, in that case, what happened is that the probabilities of and were swapped from and to and respectively, which is exactly what the quantum NOT gate does.
So from this we understand more concretely what "the gate only operates if the first qubit is set to one" means.
Now go and study the Bell state and understand intuitively how this gate is used to produce it.
Controlled quantum gates are gates that have two types of input qubits:These gates can be understood as doing a certain unitary operation only if the control qubits are enabled or disabled.
- control qubits
- operand qubits (terminology made up by Ciro Santilli just now)
The first example to look at is the CNOT gate.
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}
Course plan:
- Section "Programmer's model of quantum computers"
- look at a Qiskit hello world
- e.g. ours: qiskit/hello.py
- learn about quantum circuits.
- tensor product in quantum computing
- First we learn some quantum logic gates. This shows an alternative, and extremely important view of a quantum computer besides a matrix multiplication: as a circuit. Fundamental subsections:
- quantum algorithms
Uses photons!
The key experiment/phenomena that sets the basis for photonic quantum computing is the two photon interference experiment.
The physical representation of the information encoding is very easy to understand:
- input: we choose to put or not photons into certain wires or no
- interaction: two wires pass very nearby at some point, and photons travelling on either of them can jump to the other one and interact with the other photons
- output: the probabilities that photos photons will go out through one wire or another
In this example we will initialize a quantum circuit with a single CNOT gate and see the output values.
By default, Qiskit initializes every qubit to 0 as shown in the qiskit/hello.py. But we can also initialize to arbitrary values as would be done when computing the output for various different inputs.
Output:which we should all be able to understand intuitively given our understanding of the CNOT gate and quantum state vectors.
┌──────────────────────┐
q_0: ┤0 ├──■──
│ Initialize(1,0,0,0) │┌─┴─┐
q_1: ┤1 ├┤ X ├
└──────────────────────┘└───┘
c: 2/═════════════════════════════
init: [1, 0, 0, 0]
probs: [1. 0. 0. 0.]
init: [0, 1, 0, 0]
probs: [0. 0. 0. 1.]
init: [0, 0, 1, 0]
probs: [0. 0. 1. 0.]
init: [0, 0, 0, 1]
probs: [0. 1. 0. 0.]
┌──────────────────────────────────┐
q_0: ┤0 ├──■──
│ Initialize(0.70711,0,0,0.70711) │┌─┴─┐
q_1: ┤1 ├┤ X ├
└──────────────────────────────────┘└───┘
c: 2/═════════════════════════════════════════
init: [0.7071067811865475, 0, 0, 0.7071067811865475]
probs: [0.5 0.5 0. 0. ]
quantumcomputing.stackexchange.com/questions/13202/qiskit-initializing-n-qubits-with-binary-values-0s-and-1s describes how to initialize circuits qubits only with binary 0 or 1 to avoid dealing with the exponential number of elements of the quantum state vector.
Quantum logic gates are needed for physical implementation Updated 2024-12-15 +Created 1970-01-01
One direct practical reason is that we need to map the matrix to real quantum hardware somehow, and all quantum hardware designs so far and likely in the future are gate-based: you manipulate a small number of qubits at a time (2) and add more and more of such operations.
While there are "quantum compilers" to increase the portability of quantum programs, it is to be expected that programs manually crafted for a specific hardware will be more efficient just like in classic computers.
TODO: is there any clear reason why computers can't beat humans in approximating any unitary matrix with a gate set?
This is analogous to what classic circuit programmers will do, by using smaller logic gates to create complex circuits, rather than directly creating one huge truth table.
The most commonly considered quantum gates take 1, 2, or 3 qubits as input.
The gates themselves are just unitary matrices that operate on the input qubits and produce the same number of output qubits.
For example, the matrix for the CNOT gate, which takes 2 qubits as input is:
1 0 0 0
0 1 0 0
0 0 0 1
0 0 1 0
The final question is then: if I have a 2 qubit gate but an input with more qubits, say 3 qubits, then what does the 2 qubit gate (4x4 matrix) do for the final big 3 qubit matrix (8x8)? In order words, how do we scale quantum gates up to match the total number of qubits?
The intuitive answer is simple: we "just" extend the small matrix with a larger identity matrix so that the sum of the probabilities third bit is unaffected.
More precisely, we likely have to extend the matrix in a way such that the partial measurement of the original small gate qubits leaves all other qubits unaffected.
For example, if the circuit were made up of a CNOT gate operating on the first and second qubits as in:
0 ----+----- 0
|
1 ---CNOT--- 1
2 ---------- 2
then we would just extend the 2x2 CNOT gate to:
TODO lazy to properly learn right now. Apparently you have to use the Kronecker product by the identity matrix. Also, zX-calculus appears to provide a powerful alternative method in some/all cases.
We don't need to understand a super generalized version of tensor products to know what they mean in basic quantum computing!
Intuitively, taking a tensor product of two qubits simply means putting them together on the same quantum system/computer.
The tensor product is called a "product" because it distributes over addition.
E.g. consider:
Intuitively, in this operation we just put a Hadamard gate qubit together with a second pure qubit.
And the outcome still has the second qubit as always 0, because we haven't made them interact.
The quantum state is called a separable state, because it can be written as a single product of two different qubits. We have simply brought two qubits together, without making them interact.
If we then add a CNOT gate to make a Bell state:we can now see that the Bell state is non-separable: we've made the two qubits interact, and there is no way to write this state with a single tensor product. The qubits are fundamentally entangled.