ProductBlogAlgorithmsAboutSign inSign up

Quick Example

Quantum Fourier Transform: Quantum Circuit Example

Building block of many algorithms

What this example teaches

Apply a three-qubit Fourier transform to the fixed input |101>.

Expected result

For input |101>, amplitude y is exp(2πi·5y/8)/sqrt(8) in q0-first wire order. All eight basis outcomes have probability 1/8; uniform counts alone do not verify these relative phases.

Browser bit strings list physical wires q0 first, including auxiliary wires. Native classical-register strings can use another order; inspect the declared measurements.

These are ideal probabilities. Sampled frequencies fluctuate when more than one outcome is possible; they need not match percentages exactly. An ideal outcome with probability 100% has no finite-shot variation. Hardware noise is a separate effect.

Try the example

Open Run in Marqov →

The link opens the editor. Save existing work, then choose Examples → Quantum Fourier Transform → framework. Click Run, choose an available browser simulator, then click Run in browser. The link does not preselect or execute the example.

Scope and limitations

  • This example transforms one fixed three-qubit input. Uniform measurement counts do not verify its Fourier phases.

These expected answers describe the fixed circuit. This page does not establish a completed hosted or hardware run. Read the user guide for this example.

Inspect and download the circuit

Inspect the same fixed circuit in five frameworks. Browser execution translates supported circuit definitions into OpenQASM; it does not run general Python. Outside Marqov, Qiskit, Cirq, and Marqov snippets need a separate simulator invocation. PennyLane invokes its QNode and stores sampled counts in result.

OpenQASM
// Quantum Fourier Transform — 3-qubit QFT
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];

// Prepare input state |101> = 5
x q[0];
x q[2];

// QFT (cu1 is qelib1's controlled phase: diag(1, 1, 1, e^{i*lambda}))
h q[0];
cu1(pi/2) q[1],q[0];
cu1(pi/4) q[2],q[0];
h q[1];
cu1(pi/2) q[2],q[1];
h q[2];

// Swap for correct output order (three cx: swap is not in qelib1.inc)
cx q[0],q[2];
cx q[2],q[0];
cx q[0],q[2];

measure q -> c;
Download OpenQASM circuit
Qiskit
# Quantum Fourier Transform — 3-qubit QFT
from qiskit import QuantumCircuit
import math

qc = QuantumCircuit(3, 3)

# Prepare input state |101> = 5
qc.x([0, 2])

# QFT
qc.h(0)
qc.cp(math.pi / 2, 1, 0)
qc.cp(math.pi / 4, 2, 0)
qc.h(1)
qc.cp(math.pi / 2, 2, 1)
qc.h(2)

# Swap for correct output order
qc.swap(0, 2)

qc.measure([0, 1, 2], [0, 1, 2])
Download Qiskit circuit
Cirq
# Quantum Fourier Transform — 3-qubit QFT
import cirq
import numpy as np

q = cirq.LineQubit.range(3)
circuit = cirq.Circuit([
    # Prepare input state |101> = 5
    cirq.X(q[0]),
    cirq.X(q[2]),
    # QFT
    cirq.H(q[0]),
    cirq.CZPowGate(exponent=1/2)(q[1], q[0]),
    cirq.CZPowGate(exponent=1/4)(q[2], q[0]),
    cirq.H(q[1]),
    cirq.CZPowGate(exponent=1/2)(q[2], q[1]),
    cirq.H(q[2]),
    # Swap for correct output order
    cirq.SWAP(q[0], q[2]),
    cirq.measure(*q, key="result"),
])
Download Cirq circuit
PennyLane
# Quantum Fourier Transform — 3-qubit QFT
import pennylane as qml
import numpy as np

dev = qml.device("default.qubit", wires=3, shots=1000)

@qml.qnode(dev)
def qft():
    # Prepare input state |101> = 5
    qml.PauliX(wires=0)
    qml.PauliX(wires=2)
    # QFT
    qml.Hadamard(wires=0)
    qml.ControlledPhaseShift(np.pi / 2, wires=[1, 0])
    qml.ControlledPhaseShift(np.pi / 4, wires=[2, 0])
    qml.Hadamard(wires=1)
    qml.ControlledPhaseShift(np.pi / 2, wires=[2, 1])
    qml.Hadamard(wires=2)
    # Swap
    qml.SWAP(wires=[0, 2])
    return qml.counts()

result = qft()
Download PennyLane circuit
Marqov
# Quantum Fourier Transform — 3-qubit QFT
from marqov import Circuit
import math

# Circuit has no controlled-phase method, so each cp(theta) on control c,
# target t is written exactly (up to a global phase) with the SDK's gates:
# rz(theta/2) c, rz(theta/2) t, cnot(c, t), rz(-theta/2) t, cnot(c, t).
circuit = (
    Circuit()
    .x(0).x(2)  # Input |101> = 5
    .h(0)
    .rz(math.pi / 4, 1).rz(math.pi / 4, 0).cnot(1, 0).rz(-math.pi / 4, 0).cnot(1, 0)  # cp(pi/2) 1,0
    .rz(math.pi / 8, 2).rz(math.pi / 8, 0).cnot(2, 0).rz(-math.pi / 8, 0).cnot(2, 0)  # cp(pi/4) 2,0
    .h(1)
    .rz(math.pi / 4, 2).rz(math.pi / 4, 1).cnot(2, 1).rz(-math.pi / 4, 1).cnot(2, 1)  # cp(pi/2) 2,1
    .h(2)
    .swap(0, 2)
)
Download Marqov circuit

Sources and provenance

These references explain the concept and gate conventions. The snippets originate in Marqov's internal example collection; an upstream code attribution has not been established.

Snapshot identity
Source revision
df5a80cd474144684bf69dbda237b28ce40cc48c
Example version
f7b1dc61f4caf4912dce5b16695f50e1b0930aedb991fb99d8fb98f031c429d4

The source revision identifies the editor collection used for this snapshot. The example version identifies its five exact code variants; neither is evidence of a hosted run.

Go deeper

For a paper-based experiment with scientific comparisons and execution evidence, read Research Replications. Its published scope and availability are separate from this small teaching circuit.

Explore all twelve Quick Examples →