Quantum-Mechanics
Quantum gates, state representations, fidelity metrics, noise channels, and error-correcting codes for qubit-based computation.
Namespace: CSharpNumerics.Physics.Quantum
using CSharpNumerics.Physics.Quantum;
using CSharpNumerics.Physics.Quantum.NoiseModels;
using CSharpNumerics.Physics.Quantum.ErrorCorrection;
โ๏ธ Quantum Gatesโ
Namespace: CSharpNumerics.Physics.Quantum
Quantum gates are unitary operators acting on one or more qubits. Each gate is represented by a ComplexMatrix and knows how to apply itself to a ComplexVectorN state vector.
All gates extend the abstract QuantumGate base class:
| Class | Qubits | Matrix | Description |
|---|---|---|---|
HadamardGate | 1 | Creates equal superposition | |
PauliXGate | 1 | Quantum NOT โ flips |0โฉ โ |1โฉ | |
PauliYGate | 1 | Bit + phase flip, | |
PauliZGate | 1 | Phase flip โ |1โฉ โ โ|1โฉ | |
SGate | 1 | Phase gate (ฯ/2), | |
TGate | 1 | ฯ/8 gate, | |
PhaseGate(ฮธ) | 1 | General phase gate, | |
RxGate(ฮธ) | 1 | Rotation about X-axis | |
RyGate(ฮธ) | 1 | Rotation about Y-axis | |
RzGate(ฮธ) | 1 | Rotation about Z-axis | |
CNOTGate | 2 | 4ร4 permutation | Flips target qubit when control is |1โฉ |
CZGate | 2 | Phase flip on |11โฉ | |
CPhaseGate(ฮธ) | 2 | Controlled phase, | |
SWAPGate | 2 | 4ร4 permutation | Swaps two qubit states |
ToffoliGate | 3 | 8ร8 permutation | CCNOT โ flips target when both controls are |1โฉ |
FredkinGate | 3 | 8ร8 permutation | CSWAP โ swaps targets when control is |1โฉ |
PhaseOracle(n, states) | n | 2โฟร2โฟ diagonal | Flips phase of marked basis states: |wโฉ โ โ|wโฉ |
ControlledGate(U) | n+1 | 2โฟโบยนร2โฟโบยน block | Applies U when control qubit is |1โฉ |
ModularMultiplyGate(a,N,n) | n | 2โฟร2โฟ permutation | |yโฉ โ |ay mod Nโฉ, used in Shor's algorithm |
QuantumGate (abstract)
public abstract class QuantumGate
{
public abstract int QubitCount { get; }
public abstract ComplexMatrix GetMatrix();
public ComplexVectorN Apply(ComplexVectorN amplitudes, int[] qubitIndices, int totalQubits);
}
Apply uses the gate's unitary matrix to transform the relevant amplitudes in-place (tensor-product expansion) โ works for arbitrary qubit counts and target indices.
Usage
Gates are consumed by QuantumInstruction and QuantumCircuit in the Engines Quantum section:
using CSharpNumerics.Physics.Quantum;
using CSharpNumerics.Engines.Quantum;
var circuit = new QuantumCircuit(2);
circuit.AddInstruction(new QuantumInstruction(new HadamardGate(), new List<int> { 0 }));
circuit.AddInstruction(new QuantumInstruction(new CNOTGate(), new List<int> { 0, 1 }));
var state = new QuantumSimulator().Run(circuit); // Bell state (|00โฉ + |11โฉ)/โ2
๐ BlochVectorโ
Represents a single-qubit pure state on the Bloch sphere. Given :
using CSharpNumerics.Physics.Quantum;
using CSharpNumerics.Engines.Quantum;
// From a circuit result
var circuit = new QuantumCircuit(1);
circuit.AddInstruction(new QuantumInstruction(new HadamardGate(), new List<int> { 0 }));
var state = new QuantumSimulator().Run(circuit);
BlochVector bloch = state.GetBlochVector(); // (1, 0, 0) โ +X axis
double theta = bloch.Theta; // polar angle ฮธ
double phi = bloch.Phi; // azimuthal angle ฯ
double r = bloch.Radius; // 1.0 for pure states
Vector v = bloch.ToVector(); // 3D Vector for rendering
// Directly from amplitudes
var b = BlochVector.FromAmplitudes(
new ComplexNumber(1, 0), new ComplexNumber(0, 0)); // |0โฉ โ (0, 0, 1)
| Property | Description |
|---|---|
X, Y, Z | Cartesian Bloch coordinates |
Theta | Polar angle from +Z |
Phi | Azimuthal angle |
Radius | Vector length (1 for pure states) |
ToVector() | Returns a 3D Vector for visualization |
Canonical states on the sphere:
| State | Bloch |
|---|---|
| โ north pole | |
| โ south pole | |
| โ +X | |
| โ โX | |
| โ +Y | |
| โ โY |
๐ QuantumFidelityโ
Static methods for computing the overlap between quantum states. Fidelity ranges from 0 (orthogonal) to 1 (identical).
State fidelity โ
using CSharpNumerics.Physics.Quantum;
using CSharpNumerics.Engines.Quantum;
var sim = new QuantumSimulator();
var s0 = sim.Run(QuantumCircuitBuilder.New(1).Build()); // |0โฉ
var sPlus = sim.Run(QuantumCircuitBuilder.New(1).H(0).Build()); // |+โฉ
var s1 = sim.Run(QuantumCircuitBuilder.New(1).X(0).Build()); // |1โฉ
double f1 = QuantumFidelity.Fidelity(s0, s0); // 1.0 โ identical
double f2 = QuantumFidelity.Fidelity(s0, sPlus); // 0.5 โ overlap
double f3 = QuantumFidelity.Fidelity(s0, s1); // 0.0 โ orthogonal
Vector fidelity โ works directly on ComplexVectorN
double f = QuantumFidelity.Fidelity(psi, phi); // |โจฯ|ฯโฉ|ยฒ
Bloch fidelity โ geometric formula for single-qubit states:
var b1 = s1State.GetBlochVector();
var b2 = s2State.GetBlochVector();
double f = QuantumFidelity.BlochFidelity(b1, b2);
// Matches state fidelity for pure single-qubit states
๐ Noise Modelsโ
The Physics.Quantum.NoiseModels namespace provides quantum noise channels using the Kraus operator formalism. Each channel implements INoiseChannel and satisfies the completeness relation .
| Channel | Parameter | Kraus Operators | Physical Model |
|---|---|---|---|
DepolarizingNoise(p) | , | Random Pauli error โ qubit replaced by maximally mixed state with probability | |
DephasingNoise(p) | , | Phase-flip error โ decoherence | |
AmplitudeDampingNoise(ฮณ) | , | Energy dissipation โ decay () |
Usage with NoisyQuantumSimulator (from CSharpNumerics.Engines.Quantum):
using CSharpNumerics.Physics.Quantum.NoiseModels;
using CSharpNumerics.Engines.Quantum;
var circuit = QuantumCircuitBuilder.New(2).H(0).CNOT(0, 1).Build();
// Ideal simulation
var ideal = new QuantumSimulator().Run(circuit);
// Noisy simulation โ channels stacked, applied after each gate
var noisy = new NoisyQuantumSimulator(new Random(42))
.WithNoise(new DepolarizingNoise(0.01))
.WithNoise(new AmplitudeDampingNoise(0.005))
.Run(circuit);
double fidelity = QuantumFidelity.Fidelity(ideal, noisy);
INoiseChannel interface
public interface INoiseChannel
{
int QubitCount { get; }
ComplexMatrix[] GetKrausOperators();
}
๐ก๏ธ Quantum Error Correction โ Code Definitionsโ
Namespace: CSharpNumerics.Physics.Quantum.ErrorCorrection
The ErrorCorrection sub-namespace defines quantum error-correcting codes as mathematical objects โ stabilizers, correction maps, and logical operators. The simulation/orchestration layer lives in Engines.Quantum.ErrorCorrection.
IQuantumErrorCorrectionCodeโ
Interface for any [[n, k, d]] stabilizer code:
| Property / Method | Description |
|---|---|
PhysicalQubits | Number of physical qubits (n) |
LogicalQubits | Number of logical qubits (k) |
Distance | Code distance (d) |
SyndromeQubits | Number of ancilla qubits for syndrome extraction |
GetStabilizers() | Returns stabilizer generators as (qubit, Pauli) lists |
GetCorrectionMap() | Syndrome integer โ corrective (qubit, Pauli) operations |
GetLogicalX(i) | Logical Xฬ operator for logical qubit i |
GetLogicalZ(i) | Logical Zฬ operator for logical qubit i |
BitFlipCode3 โ [[3,1,1]]โ
Encodes into . Corrects any single-qubit X (bit-flip) error.
Stabilizers: ,
| Syndrome | Error | Correction |
|---|---|---|
| 00 | None | โ |
| 01 | Apply X to qubit 0 | |
| 10 | Apply X to qubit 2 | |
| 11 | Apply X to qubit 1 |
Logical operators: ,
PhaseFlipCode3 โ [[3,1,1]]โ
Encodes into . Corrects any single-qubit Z (phase-flip) error.
Stabilizers: ,
| Syndrome | Error | Correction |
|---|---|---|
| 00 | None | โ |
| 01 | Apply Z to qubit 0 | |
| 10 | Apply Z to qubit 2 | |
| 11 | Apply Z to qubit 1 |
Logical operators: ,
using CSharpNumerics.Physics.Quantum.ErrorCorrection;
var code = new BitFlipCode3();
var stabs = code.GetStabilizers(); // [{(0,'Z'),(1,'Z')}, {(1,'Z'),(2,'Z')}]
var map = code.GetCorrectionMap(); // 0โ[], 1โ[(0,'X')], 2โ[(2,'X')], 3โ[(1,'X')]
var logX = code.GetLogicalX(); // [(0,'X'),(1,'X'),(2,'X')]
ShorCode9 โ [[9,1,3]]โ
Shor's 9-qubit code โ the first code to correct any single-qubit error (X, Z, or Y). Uses three blocks of three qubits: inner bit-flip repetition within blocks, outer phase-flip repetition across blocks.
8 stabilizer generators:
| Generator | Operator | Type |
|---|---|---|
| within each block | Bit-flip detection | |
| Phase-flip detection (blocks 0โ1) | ||
| Phase-flip detection (blocks 1โ2) |
Correction: The 8-bit syndrome (256 values) maps to at most one X correction + one Z correction. Each block's 2-bit sub-syndrome identifies bit-flip errors exactly as in BitFlipCode3. The 2-bit phase-flip sub-syndrome identifies which block suffered a Z error.
Logical operators: ,
var shor = new ShorCode9();
var stabs = shor.GetStabilizers(); // 8 generators
var map = shor.GetCorrectionMap(); // 256 syndrome entries
SteaneCode7 โ [[7,1,3]]โ
The Steane code โ the smallest CSS (Calderbank-Shor-Steane) code, correcting any single-qubit error using only 7 physical qubits. Built from the classical [7,4,3] Hamming code and its dual [7,3,4] code.
where is the [7,3,4] dual Hamming code and .
6 stabilizer generators:
| Generator | Operator | Type |
|---|---|---|
| X-error detection | ||
| X-error detection | ||
| X-error detection | ||
| Z-error detection | ||
| Z-error detection | ||
| Z-error detection |
Correction: The 6-bit syndrome has two independent 3-bit halves โ Z-stabilizer syndrome (bits 0โ2) identifies X errors, X-stabilizer syndrome (bits 3โ5) identifies Z errors, using the Hamming decoding map: syndrome qubit where column of the parity-check matrix equals the binary expansion of .
Logical operators: ,
var steane = new SteaneCode7();
var stabs = steane.GetStabilizers(); // 6 generators (3 Z-type + 3 X-type)
var map = steane.GetCorrectionMap(); // 64 syndrome entries