We present several quantum computing algorithms. Each file is a standalone example.
First, perform generic setup as follows.
cd <path>/quantum
python3.8 -m venv ./.venv
source ./.venv/bin/activate
pip install --upgrade pip
pip install qiskit[visualization]
pip install qiskit-ibm-runtime
pip install matplotlib pylatexenc ipykernel
Users are recommended to run each of this repository's code files inside the interactive window in VSCode.
Alternatively, users may run the code inside JupyterLab. Follow the additional steps below to install and launch JupyterLab.
cd <path>/quantum
pip install jupyterlab
# Once installed, launch JupyterLab with:
jupyter lab
Optional libraries for code formatting.
cd <path>/quantum
pip install black[jupyter] isort
# Execute to format code.
make format
-
Compare the states of two single-qubit registers. If the two input states are equal, the output register results in
$|1⟩$ state. An useful interpretation is to see that the probability of a$|1⟩$ outcome is a measure of just how identical the two inputs are. -
Alice teleports the quantum state of her payload qubit using an entangled pair of qubits shared with Bob. Only two classical bits are needed to transmit Alice’s qubit state (i.e., magnitudes and relative phase) and Bob's retrieved qubit state will be correct to a potentially infinite number of classical bits of precision. Because a traditional channel is needed to convey the two classical bits from Alice to Bob, the speed of teleportation can be no faster than the speed of light.
To verify successful teleportation, Bob applies the gates, which Alice applied on
$|0⟩$ to prepare her payload, to his retrieved qubit in reverse. If Bob's retrieved qubit matches that sent by Alice, the final measurement result after verification gates should always be0
in a perfect quantum circuit. -
Create two quantum registers and initialize them to
$a=\sqrt{0.5}|1⟩+\sqrt{0.5}|5⟩$ and$b=\sqrt{0.5}|1⟩+e^{i\pi/4}\sqrt{0.5}|3⟩$ . Decrement register$a$ by 3. Then, increment register$b$ conditional on register$a<0$ . Here, register$a$ is assumed to be in two’s-complement, where the highest-order bit indicates the sign. Finally, increment register$a$ by 3. -
Scratch qubits play a temporary role in enabling quantum operations. A specific example of an otherwise irreversible operation that can be made reversible with a scratch qubit is
abs(a)
. Theabs()
function computes the absolute value of a signed integer. We assume two’s-complement notation here.In this example,
abs
of a quantum registera
is computed. Then, addabs(a)
to another quantum registerb
. Finally, uncompute (i.e., reverse the operations on) the scratch qubit and quantum registera
to return them to their initial states. -
Amplitude amplification converts inaccessible phase differences inside a quantum processor into measurable magnitude differences. Amplitue amplification consists of iterative
flip
followed bymirror
subroutines. Subroutineflip
marks the desired state by a phase-flip. Subroutinemirror
reflects each state about the average overall state. This results in the marked state having a larger read probability than nonmarked states. In Grover search algorithm, theflip
andmirror
are known as theoracle
anddiffuser
, respectively. -
Use Quantum Fourier Transform to deduce the frequencies present in a quantum register.
-
We build a phase estimation circuit to compute the eigenphase
$\theta$ , given a unitary quantum operation$U$ and its eigenstate. Acting an$U$ on its eigenstate produces the same eigenstate but with the eigenphase applied to its global phase. That is to say$U|\psi⟩=e^{i2\pi\theta}|\psi⟩$ . These eigenphase rotations are kicked-back into the$m$ qubits in the counting register, creating a frequency modulation. Inverse QFT is applied to the counting register to decode the frequency present and to obtain its count value$v$ . Then,$\theta = v2\pi / 2^m$ .A superposition of eigenstates as an input to the phase estimation results in a superposition of the associated eigenphases in the output. The magnitude for each eigenphase in the output superposition will be precisely the magnitude that its corresponding eigenstate had in the input register.
-
Phase-logic circuits flip the relative phases of all input states for which the circuit operation evaluate to true. Note that phase-logic circuits take in magnitude values such as
$|1⟩$ or$|2⟩$ , but not phase values, as inputs and encode output values in the relative phases.Here, we solve a satisfiable 3-SAT problem by finding the combination of boolean inputs
a
,b
,c
which produce a 1 output from the boolean statement(a OR b) AND ((NOT a) OR c) AND ((NOT b) OR (NOT c)) AND (a OR c)
.General recipe to solve satisfiability problems using phase logic is as follows.
- Initialize the quantum register in a uniform superposition.
- Transform the boolean statement into a number of clauses that have to be satisfied simultaneously, such that the final statement is the
AND
of a number of independent clauses. - Represent each individual clause using magnitude logic, storing the output in scratch qubits.
- Perform a phase-logic
AND
between all the scratch qubits to combine the different clauses. - Uncompute all of the magnitude-logic operations, returning the scratch qubits to their initial states.
- Finally, perform
mirror
subroutine, from the amplitude amplification algorithm.
Perform the above phase
flip
(i.e., magnitude-logic and phase-logic operations) andmirror
subroutines, as many times as necessary before reading out the quantum register. -
Quantum circuit must be transpiled for it to run on real devices. Qiskit's transpiler converts circuit operations to those supported by the device, maps qubits according to the device's coupling map, and performs some optimization of circuit’s gate count.
Here, we transpile a quantum circuit containing
[cx, y]
gates onto aGenericBackendV2
device which only supports[id, rz, sx, x, cx]
gates. Circuit before (left) and after (right) transpiling is shown above. We may selectoptimization_level
$\in [0,1,2,3]$ as input argument to the transpiler. Level 0 does the aboslute minimum necessary, level 1 is the default setting, level 2 tries to avoid qubit swaps, and level 3 is the highest which uses smarter algorithms to cancel out gates.