numerics
Loading...
Searching...
No Matches
num::quantum Namespace Reference

Typedefs

using Statevector = num::CVector
 Dense statevector: complex vector of length 2^n_qubits, backed by num::CVector.
 
using Gate = std::array< cplx, 4 >
 2x2 single-qubit gate stored row-major: { G[0,0], G[0,1], G[1,0], G[1,1] }
 
using Gate2Q = std::array< cplx, 16 >
 4x4 two-qubit gate stored row-major (16 entries)
 
using cplx = std::complex< real >
 

Functions

Statevector zero_state (int n_qubits)
 Returns |0...0> computational basis state for n_qubits qubits.
 
Statevector basis_state (int n_qubits, int k)
 Returns the computational basis state |k> for an n_qubits system.
 
Gate gate_x ()
 Pauli-X (NOT)
 
Gate gate_y ()
 Pauli-Y.
 
Gate gate_z ()
 Pauli-Z.
 
Gate gate_h ()
 Hadamard.
 
Gate gate_s ()
 Phase gate S = diag(1, i)
 
Gate gate_sdg ()
 S† = diag(1, -i)
 
Gate gate_t ()
 T gate = diag(1, e^{ipi/4})
 
Gate gate_tdg ()
 T† gate.
 
Gate gate_rx (real theta)
 Rotation about X: R_x(theta) = exp(-ithetaX/2)
 
Gate gate_ry (real theta)
 Rotation about Y: R_y(theta) = exp(-ithetaY/2)
 
Gate gate_rz (real theta)
 Rotation about Z: R_z(theta) = exp(-ithetaZ/2)
 
Gate gate_p (real lambda)
 Phase gate: diag(1, e^{ilambda})
 
Gate gate_u (real theta, real phi, real lambda)
 General SU(2) gate.
 
Gate2Q gate2q_cnot ()
 CNOT (qubit 0 = control, qubit 1 = target in subspace)
 
Gate2Q gate2q_cz ()
 Controlled-Z.
 
Gate2Q gate2q_swap ()
 SWAP.
 
void apply_1q (Statevector &sv, int n_qubits, const Gate &G, int target)
 Apply a 2x2 gate to a single qubit in-place.
 
void apply_2q (Statevector &sv, int n_qubits, const Gate2Q &G, int q0, int q1)
 Apply a general 4x4 two-qubit gate to qubits q0, q1.
 
void apply_cnot (Statevector &sv, int n_qubits, int control, int target)
 CNOT with explicit control and target qubits.
 
void apply_cz (Statevector &sv, int n_qubits, int control, int target)
 Controlled-Z.
 
void apply_swap (Statevector &sv, int n_qubits, int q0, int q1)
 SWAP two qubits.
 
void apply_toffoli (Statevector &sv, int n_qubits, int c0, int c1, int target)
 Toffoli (CCX) gate.
 
void apply_cy (Statevector &sv, int n_qubits, int control, int target)
 Controlled-Y.
 
void apply_cp (Statevector &sv, int n_qubits, real lambda, int control, int target)
 Controlled phase: applies phase e^{ilambda} when both control=1 and target=1.
 
void apply_cswap (Statevector &sv, int n_qubits, int ctrl, int q0, int q1)
 Fredkin (CSWAP): swaps q0 and q1 when ctrl=1.
 
real norm (const Statevector &sv)
 L2 norm of the statevector (should be 1 for a valid quantum state)
 
void normalize (Statevector &sv)
 Normalize statevector in-place.
 
std::vector< realprobabilities (const Statevector &sv)
 Measurement probability for each basis state: p_i = |a_i|^2.
 
real expectation (const Statevector &sv, std::function< void(const Statevector &, Statevector &)> op)
 Expectation value <psi|O|psi> for a Hermitian operator given as matrix-free matvec.
 
real expectation_z (const Statevector &sv, int qubit)
 <Z_q> = P(q=0) - P(q=1) for qubit q
 
real expectation_x (const Statevector &sv, int n_qubits, int qubit)
 <X_q> for qubit q
 
real expectation_y (const Statevector &sv, int n_qubits, int qubit)
 <Y_q> for qubit q
 
std::array< std::complex< real >, 4 > reduced_density_matrix (const Statevector &sv, int n_qubits, int qubit)
 Reduced density matrix for a single qubit (2x2, returned row-major)
 
real entanglement_entropy (const Statevector &sv, int n_qubits, int qubit)
 Von Neumann entropy of a single qubit's reduced density matrix.
 

Typedef Documentation

◆ cplx

Definition at line 7 of file statevector.cpp.

◆ Gate

2x2 single-qubit gate stored row-major: { G[0,0], G[0,1], G[1,0], G[1,1] }

Definition at line 31 of file statevector.hpp.

◆ Gate2Q

4x4 two-qubit gate stored row-major (16 entries)

Definition at line 34 of file statevector.hpp.

◆ Statevector

Dense statevector: complex vector of length 2^n_qubits, backed by num::CVector.

Definition at line 28 of file statevector.hpp.

Function Documentation

◆ apply_1q()

void num::quantum::apply_1q ( Statevector sv,
int  n_qubits,
const Gate G,
int  target 
)

Apply a 2x2 gate to a single qubit in-place.

Parameters
svStatevector of length 2^n_qubits (modified in-place)
n_qubitsTotal number of qubits
G2x2 gate (row-major)
targetTarget qubit index (0 = LSB)

Definition at line 107 of file statevector.cpp.

References num::ipow().

Referenced by expectation_x(), and expectation_y().

◆ apply_2q()

void num::quantum::apply_2q ( Statevector sv,
int  n_qubits,
const Gate2Q G,
int  q0,
int  q1 
)

Apply a general 4x4 two-qubit gate to qubits q0, q1.

Parameters
svStatevector
n_qubitsTotal number of qubits
G4x4 gate acting on (q0, q1) in little-endian order
q0First qubit (LSB of the 2-qubit subspace)
q1Second qubit (MSB of the 2-qubit subspace)

Definition at line 122 of file statevector.cpp.

References num::ipow().

◆ apply_cnot()

void num::quantum::apply_cnot ( Statevector sv,
int  n_qubits,
int  control,
int  target 
)

CNOT with explicit control and target qubits.

Definition at line 139 of file statevector.cpp.

References num::ipow().

◆ apply_cp()

void num::quantum::apply_cp ( Statevector sv,
int  n_qubits,
real  lambda,
int  control,
int  target 
)

Controlled phase: applies phase e^{ilambda} when both control=1 and target=1.

Definition at line 277 of file statevector.cpp.

References num::ipow().

◆ apply_cswap()

void num::quantum::apply_cswap ( Statevector sv,
int  n_qubits,
int  ctrl,
int  q0,
int  q1 
)

Fredkin (CSWAP): swaps q0 and q1 when ctrl=1.

Definition at line 288 of file statevector.cpp.

References num::ipow().

◆ apply_cy()

void num::quantum::apply_cy ( Statevector sv,
int  n_qubits,
int  control,
int  target 
)

Controlled-Y.

Definition at line 262 of file statevector.cpp.

References num::ipow().

◆ apply_cz()

void num::quantum::apply_cz ( Statevector sv,
int  n_qubits,
int  control,
int  target 
)

Controlled-Z.

Definition at line 148 of file statevector.cpp.

References num::ipow().

◆ apply_swap()

void num::quantum::apply_swap ( Statevector sv,
int  n_qubits,
int  q0,
int  q1 
)

SWAP two qubits.

Definition at line 157 of file statevector.cpp.

References num::ipow().

◆ apply_toffoli()

void num::quantum::apply_toffoli ( Statevector sv,
int  n_qubits,
int  c0,
int  c1,
int  target 
)

Toffoli (CCX) gate.

Definition at line 166 of file statevector.cpp.

References num::ipow().

◆ basis_state()

Statevector num::quantum::basis_state ( int  n_qubits,
int  k 
)

Returns the computational basis state |k> for an n_qubits system.

Definition at line 18 of file statevector.cpp.

References num::ipow().

◆ entanglement_entropy()

real num::quantum::entanglement_entropy ( const Statevector sv,
int  n_qubits,
int  qubit 
)

Von Neumann entropy of a single qubit's reduced density matrix.

Definition at line 248 of file statevector.cpp.

References num::e, num::ipow(), and reduced_density_matrix().

Referenced by main().

◆ expectation()

real num::quantum::expectation ( const Statevector sv,
std::function< void(const Statevector &, Statevector &)>  op 
)

Expectation value <psi|O|psi> for a Hermitian operator given as matrix-free matvec.

Definition at line 192 of file statevector.cpp.

References num::ipow(), and num::BasicVector< T >::size().

◆ expectation_x()

real num::quantum::expectation_x ( const Statevector sv,
int  n_qubits,
int  qubit 
)

<X_q> for qubit q

Definition at line 211 of file statevector.cpp.

References apply_1q(), gate_x(), num::ipow(), and num::BasicVector< T >::size().

◆ expectation_y()

real num::quantum::expectation_y ( const Statevector sv,
int  n_qubits,
int  qubit 
)

<Y_q> for qubit q

Definition at line 220 of file statevector.cpp.

References apply_1q(), gate_y(), num::ipow(), and num::BasicVector< T >::size().

◆ expectation_z()

real num::quantum::expectation_z ( const Statevector sv,
int  qubit 
)

<Z_q> = P(q=0) - P(q=1) for qubit q

Definition at line 201 of file statevector.cpp.

References num::ipow(), and num::BasicVector< T >::size().

◆ gate2q_cnot()

Gate2Q num::quantum::gate2q_cnot ( )

CNOT (qubit 0 = control, qubit 1 = target in subspace)

Definition at line 78 of file statevector.cpp.

◆ gate2q_cz()

Gate2Q num::quantum::gate2q_cz ( )

Controlled-Z.

Definition at line 87 of file statevector.cpp.

◆ gate2q_swap()

Gate2Q num::quantum::gate2q_swap ( )

SWAP.

Definition at line 96 of file statevector.cpp.

◆ gate_h()

Gate num::quantum::gate_h ( )

Hadamard.

Definition at line 30 of file statevector.cpp.

◆ gate_p()

Gate num::quantum::gate_p ( real  lambda)

Phase gate: diag(1, e^{ilambda})

Definition at line 60 of file statevector.cpp.

◆ gate_rx()

Gate num::quantum::gate_rx ( real  theta)

Rotation about X: R_x(theta) = exp(-ithetaX/2)

Definition at line 47 of file statevector.cpp.

◆ gate_ry()

Gate num::quantum::gate_ry ( real  theta)

Rotation about Y: R_y(theta) = exp(-ithetaY/2)

Definition at line 51 of file statevector.cpp.

◆ gate_rz()

Gate num::quantum::gate_rz ( real  theta)

Rotation about Z: R_z(theta) = exp(-ithetaZ/2)

Definition at line 55 of file statevector.cpp.

◆ gate_s()

Gate num::quantum::gate_s ( )

Phase gate S = diag(1, i)

Definition at line 35 of file statevector.cpp.

◆ gate_sdg()

Gate num::quantum::gate_sdg ( )

S† = diag(1, -i)

Definition at line 36 of file statevector.cpp.

◆ gate_t()

Gate num::quantum::gate_t ( )

T gate = diag(1, e^{ipi/4})

Definition at line 38 of file statevector.cpp.

References num::pi.

◆ gate_tdg()

Gate num::quantum::gate_tdg ( )

T† gate.

Definition at line 42 of file statevector.cpp.

References num::pi.

◆ gate_u()

Gate num::quantum::gate_u ( real  theta,
real  phi,
real  lambda 
)

General SU(2) gate.

Definition at line 65 of file statevector.cpp.

References num::ipow(), and num::phi.

◆ gate_x()

Gate num::quantum::gate_x ( )

Pauli-X (NOT)

Definition at line 26 of file statevector.cpp.

Referenced by expectation_x().

◆ gate_y()

Gate num::quantum::gate_y ( )

Pauli-Y.

Definition at line 27 of file statevector.cpp.

Referenced by expectation_y().

◆ gate_z()

Gate num::quantum::gate_z ( )

Pauli-Z.

Definition at line 28 of file statevector.cpp.

◆ norm()

real num::quantum::norm ( const Statevector sv)

L2 norm of the statevector (should be 1 for a valid quantum state)

Definition at line 177 of file statevector.cpp.

References num::norm().

Referenced by main().

◆ normalize()

void num::quantum::normalize ( Statevector sv)

Normalize statevector in-place.

Definition at line 181 of file statevector.cpp.

References num::ipow(), and num::norm().

◆ probabilities()

std::vector< real > num::quantum::probabilities ( const Statevector sv)

Measurement probability for each basis state: p_i = |a_i|^2.

Definition at line 186 of file statevector.cpp.

References num::ipow(), and num::BasicVector< T >::size().

Referenced by main(), and num::Circuit::run().

◆ reduced_density_matrix()

std::array< cplx, 4 > num::quantum::reduced_density_matrix ( const Statevector sv,
int  n_qubits,
int  qubit 
)

Reduced density matrix for a single qubit (2x2, returned row-major)

Definition at line 228 of file statevector.cpp.

References num::ipow().

Referenced by entanglement_entropy().

◆ zero_state()

Statevector num::quantum::zero_state ( int  n_qubits)

Returns |0...0> computational basis state for n_qubits qubits.

Definition at line 12 of file statevector.cpp.

Referenced by num::Circuit::statevector_at().