IQC 005
Encoding
- Basis encoding:
binary srings -> computational basis states - Amplitude encoding:
data values -> amplitudes of a quantum state - Angle encoding:
data values -> rotation angles on individual qubits
Basis Encoding
- applying gates to flip qubits from to based on the binary representation of the data.
- (X gate on the second qubit)
- (X gate on the first qubit)
- Dataset superposition: to encode set
- it requires Hadamard and/or controlled gates
- Hadamard transform
import pennylane as qml
x = [int(b) for b in '1011']
def circuit(x):
qml.BasisEmbedding(features=x, wires=range(len(x)))
dev = qml.device('default.qubit', wires=4)
qnode = qml.QNode(circuit, dev)
print(qml.draw(qml.transforms.decompose(qnode), show_all_wires=True)(x))
Amplitude Encoding
- The most qubit-efficient encoding method, since qubits can encode amplitudes.
- The state can be rewritten more explicitly (for 3 qubits)
- For data points, we need qubits.
- Converting to binary
- with qubits, the computational basis states range from
- to
- can be mapped to .
- .
- with qubits, the computational basis states range from
- if
- gives (binary representation of 5)
Angle Encoding
- Each classical value controls the roation angle of a qubit gate.
- where is the rotation gate.
Summary of Encoding Methods
| Method | Qubit cost | Eample Classical Data | Use Case | Circuit Complexity |
|---|---|---|---|---|
| Basis Encoding | 1 qubit per bit | Binary strings ('1011') | Binary data, configs | Easy |
| Amplitude Encoding | qubits | Vector of real numbers | QML, optimization | Hard |
| Angle Encoding | 1 qubit per data point | Vector of angles | Variational / hybrid algorithms | Easy |
- Pennylane: BasisEmbedding, AmplitudeEmbedding, AngleEmbedding
- Qiskit: initialize
- PyTKET: StatePreparationBox
Amplitude Encoding Circuit
- The algorithm to prepare an arbitrary vector of length takes roughly that many gates. (e.g. )
wires = [0, 1, 2]
def circuit(x):
qml.AmplitudeEmbedding(x, wires)
dev = qml.device("default.qubit", wires=wires)
qnode = qml.QNode(circuit, dev)
# Random vector of length 8 (for 3 qubits)
x = np.random.rand(8)
# Normalize the vector to have unit length (required for quantum states)
x = x/np.sqrt(np.sum(x**2))
qml.draw_mpl(qnode)(x);
qml.draw_mpl(qml.transforms.decompose(qnode), decimals=3)(x);

from pytket.circuit import StatePreparationBox
from pytket.circuit.display import render_circuit_jupyter as draw
state_circ = pytket.circuit.Circuit(3)
# Example 3-qubit state to prepare
w_state = 1 / np.sqrt(3) * np.array([0, 1, 1, 0, 1, 0, 0, 0])
w_state_box = StatePreparationBox(w_state)
state_circ.add_gate(w_state_box, [0, 1, 2])
draw(state_circ)
pytket.transform.Transform.DecomposeBoxes().apply(state_circ)
draw(state_circ)

Different algorithms for Rotation gate
-
Pennylane: Transformation of quantum states using uniformly controlled rotations
-
PyTKET: Synthesis of Quantum Logic Circuits
-
Qiskit: Quantum Circuits for Isometries
-
There are concreate algorithms for encoding classical data into quantum states, but this process is generally computationally expensive.
-
It usually requires classical preprocessing, and the resulting circuit can be quite deep.
-
In general, amplitude encoding an arbitary large vector is expensive.
-
"a quantum computer can store an exponential amount of data" should be always considered together with the cost of state preparation.
-
The real advantage of quantum computers does not appear for every problem, but only for specific problems that are well-suited to them.
- Problems where state preparation is natural or efficient.
- Problems that involve simulating quantum systems themselves.
- Structured linear algebra, optimization, or sampling problems.
- Problems where the input is already given as a quantum state.
Binary Logic Gates
| a | b | sum | carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
- Sum (XOR)
- Carry (AND)
- Declare registers
- Apply opertions
- Read the result
Full-Adder in Binary Logic
- Handles three inpus (, , and carry-in ) and produces two outputs (sum and carry-out ).
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
- A full-adder = two half-adders + an OR gate
- Chaning full-adders producs a ripple-carry adder for multi-bit numbers.
Quantum Arithmetic
- All quantum gates must be Unitary (Invertable)
- A classical half-adder discards input information after computing the sum and carry.
- This irreversibility is forbidden in quantum circuits.
- The computation must be done in a way that preserves all input information.
- XOR and AND operations must be implemented using reversible gates (e.g. Toffoli gate).
- XOR CNOT gate
- AND Toffoli gate
| Classical op | Quantum gate |
|---|---|
| XOR | |
| AND |
- With a third qubit initialized to , we can compute the AND of and without losing information about and .
Half-Adder


Full-Adder


Ripple Carry Adder
- Two full-adder circuits in sequence, overlapping on the carry qubit ("rippling" the carry through the circuit).
- CDKM adder
- Carry-out is the majority vote of the three inputs

-
MAJ: Majority, computes carry in-place using only 2 CNOTs + 1 Toffoli
-
UMA: UnMajority-and-Add, reverses MAJ but overwrites with the sum.

- CDKM Ripple-Carry Adder: For two n-bit numbers, chain MAJ/UMA pairs, sharing the carry qubit.
CDKMRippleCarryAdder

Quantum Multiplication
| - | - | - | - | - | - | - |
|---|---|---|---|---|---|---|
| 1 | 1 | 0 | ||||
| x | 1 | 0 | 1 | |||
| 1 | 1 | 0 | ||||
| 0 | 0 | 0 | ||||
| + | 1 | 1 | 0 | |||
| 1 | 1 | 1 | 0 | 0 |
- Binary multiplication is "shift and add"
- for each 1-bit in the multiplier, add a shifted version of the multiplicand to the result.
- for each 0-bit, add nothing (or add a zero vector).
- In quantum, each partial addition is a conditional adder , every internal gate is controlled by a bit of the multiplier.
Superposition
- Same adder circuit works on superposition of inputs
- With one adder, both and are computed simultaneously.
- measurement collapses the superposition, we can only ever see one result per run. Either or .
- Amplifying the probability of amplitudes corresponding to correct answers is a key part of quantum algorithms.
Summary
- Basis encoding maps each classical bit to a qubit, with 0 mapped to and 1 mapped to .
- To encode 1011, we need X gates on qubits 0, 2, and 3 (assuming qubit 0 is the most significant bit).
- Applying an H gate (Hadamard gate) to each qubit puts the system in a uniform superposition over all basis states.
- Amplitude encoding of an arbitrary vector of size generally requires gates.
- In angle encoding, each classical value becomes the rotation angle of an RY gate on the -th qubit.
- Using the binary conversion formula, the decimal number 5 maps to for 3 qubits.
- In short, in quantum addition, the sum is an XOR operation, and the carry is an AND operation.
- A quantum half-adder must keep the original inputs to remain reversible and maintain its unitary nature.
- The Toffoli gate is defined as: .
- A full-adder should produce a carry-out as follows: .
- The following operation is reversible: .
- For addition "in superposition," measuring the output register yields exactly one of the partial sums, chosen probabilistically.
- The MAJ circuit computes the majority function as: .
- The QASM snippet for a half-adder uses
ccx q[0], q[1], q[2]to compute the carry () and acx q[0], q[1](orcx q[1], q[0]) gate to compute the sum (). - Quantum multiplication can be implemented via conditional addition, one per multiplier bit.