qndk.circuit¶
Gets a quantum circuit into the pipeline. Either you bring your own Qiskit circuit, or you name one from the built-in library.
Nothing here is distributed-specific — it is a thin, convenient front door onto Qiskit plus a set of benchmark circuits worth comparing against.
The library¶
QiskitCircuit(circuit_type, num_qubits=None, **kwargs) builds a named
circuit. The type is matched case-insensitively.
circuit_type |
What it is | Key kwargs |
|---|---|---|
simple |
A trivial chain, for smoke tests | — |
qft |
Quantum Fourier transform | — |
tfim |
Trotterized transverse-field Ising model | num_trotter_steps, j_coupling, h_field, dt |
grover |
Grover search for a target bitstring | target_bitstring |
gf2m |
GF(2ᵐ) finite-field arithmetic | m, operation, poly |
qv |
Quantum volume | — |
qaoa |
QAOA on a random graph | prob_edge_creation |
rca |
Ripple-carry adder | — |
mcmtv |
Multi-control multi-target gate | target, control, gate |
from qndk.circuit.qiskit_circuits import QiskitCircuit
for spec in [("qft", {"num_qubits": 6}),
("tfim", {"num_qubits": 6, "num_trotter_steps": 2}),
("grover", {"target_bitstring": "1011"}),
("gf2m", {"m": 3, "operation": "multiply"})]:
kind, kwargs = spec
circuit = QiskitCircuit(kind, **kwargs)
qc = circuit.circuit
print(f"{kind:8s} qubits={qc.num_qubits:3d} depth={qc.depth():4d} "
f"ops={dict(qc.count_ops())}")
.circuit is the underlying Qiskit QuantumCircuit, which is the escape
hatch whenever you need to do something the wrapper does not cover — adding
a gate, for instance:
from qndk.circuit.qiskit_circuits import QiskitCircuit
circuit = QiskitCircuit("tfim", num_qubits=6, num_trotter_steps=1)
print("T gates before:", circuit.circuit.count_ops().get("t", 0))
# TFIM is CX/RZ/RX only. The fault-tolerant path needs a non-Clifford gate
# to have anything to route through a magic state factory.
circuit.circuit.t(2)
print("T gates after :", circuit.circuit.count_ops().get("t", 0))
Why the examples add a t()
Several built-in circuits are Clifford-plus-rotation only, so they consume no magic states. Adding one T gate is the smallest way to exercise the magic-state machinery. See quantum_error_correction.
Bringing your own circuit¶
Every downstream stage accepts a plain Qiskit QuantumCircuit, so the
library is entirely optional:
from qiskit import QuantumCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
qc = QuantumCircuit(6)
qc.h(0)
for i in range(5):
qc.cx(i, i + 1)
qc.measure_all()
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
qpu_spec=QPUSpec(comp_qubits=2, connectivity="all2all"))
partition = KernighanLinPartitioner(qc, network).partition()
print("partitioned a hand-built circuit; epr cost:",
partition.total_epr_count)
Export¶
from qndk.circuit.qiskit_circuits import QiskitCircuit
circuit = QiskitCircuit("qft", num_qubits=4)
qasm = circuit.qasm3()
print(qasm.splitlines()[0])
print(f"{len(qasm)} bytes of QASM3")
qasm2() is also available. Both are useful for bug reports — see
Support.
Loading circuits from a module¶
CircuitLoader reads circuit definitions out of a Python module by name,
which is what the HTTP service uses to let a caller pick a circuit by name
rather than by sending code.
It expects a module importable from sys.path that defines a dict mapping
names to QuantumCircuit objects. The built-in library is not such a
module, so this is for your own circuit collections:
import sys
import textwrap
from pathlib import Path
# a module of your own, defining the dict CircuitLoader looks for
Path("my_circuits.py").write_text(textwrap.dedent('''
from qiskit import QuantumCircuit
def _bell():
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
return qc
CIRCUIT_DEFINITIONS = {"bell": _bell(), "empty": QuantumCircuit(1)}
'''))
sys.path.insert(0, ".")
from qndk.circuit.circuit_loader import CircuitLoader
loader = CircuitLoader(module_name="my_circuits",
dict_name="CIRCUIT_DEFINITIONS")
print("available:", loader.list_circuits())
print("bell depth:", loader.get_circuit("bell").depth())
get_circuit() returns None for an unknown name rather than raising, so
check the result if the name came from user input.
GF(2ᵐ) arithmetic¶
The finite-field builders are separately useful, since they produce circuits with a realistic T-gate count rather than a synthetic one.
from qndk.circuit.gf2m import GF2mField, new_gf2m, \
default_irreducible_polynomial
field = GF2mField(m=3)
print("irreducible polynomial:", bin(default_irreducible_polynomial(3)))
for operation in ("add", "multiply", "square", "inverse"):
qc = new_gf2m(3, operation=operation)
print(f"{operation:9s} qubits={qc.num_qubits:3d} "
f"depth={qc.depth():4d} t={qc.count_ops().get('t', 0):3d}")
Plotting¶
# fragment: opens windows; needs Graphviz
circuit.draw() # the circuit diagram
circuit.draw_qiskit_dag() # Qiskit's own DAG view
Next¶
The circuit goes to transpile, which normalizes its gates and builds the graph representations the partitioner needs.