Qx.Math (Qx - Quantum Computing Simulator v0.5.0)
View SourceCore mathematical and linear algebra functions for quantum mechanics calculations.
This module provides the fundamental mathematical operations needed for quantum computing simulations, including tensor products, matrix operations, and quantum state manipulations.
Summary
Functions
Applies a quantum gate (unitary matrix) to a quantum state.
Creates a computational basis state |n⟩ in a Hilbert space of given dimension.
Creates a complex number from real and imaginary parts.
Creates a complex matrix for quantum gates using c64 tensors.
Converts a complex number to an Nx tensor with [real, imag] representation.
Creates the identity matrix of given size.
Computes the inner product (dot product) of two quantum states.
Computes the Kronecker (tensor) product of two matrices.
Normalizes a quantum state vector to ensure unit magnitude.
Computes the outer product of two quantum states.
Computes the probability amplitudes from a quantum state vector.
Converts an Nx tensor with [real, imag] representation to a complex number.
Computes the trace of a matrix.
Checks if a matrix is unitary (U† U = I).
Functions
Applies a quantum gate (unitary matrix) to a quantum state.
Examples
iex> state = Nx.tensor([1.0, 0.0])
iex> x_gate = Nx.tensor([[0.0, 1.0], [1.0, 0.0]])
iex> Qx.Math.apply_gate(x_gate, state)
#Nx.Tensor<
f32[2]
[0.0, 1.0]
>
Creates a computational basis state |n⟩ in a Hilbert space of given dimension.
Examples
iex> Qx.Math.basis_state(0, 2) # |0⟩ state
#Nx.Tensor<
f32[2]
[1.0, 0.0]
>
iex> Qx.Math.basis_state(1, 2) # |1⟩ state
#Nx.Tensor<
f32[2]
[0.0, 1.0]
>
Creates a complex number from real and imaginary parts.
Examples
iex> c = Qx.Math.complex(1.0, 2.0)
iex> Complex.real(c)
1.0
iex> Complex.imag(c)
2.0
Creates a complex matrix for quantum gates using c64 tensors.
Examples
iex> # Pauli-Y gate matrix
iex> Qx.Math.complex_matrix([[0, Complex.new(0, -1)], [Complex.new(0, 1), 0]])
Converts a complex number to an Nx tensor with [real, imag] representation.
Examples
iex> c = Complex.new(1.0, 2.0)
iex> Qx.Math.complex_to_tensor(c)
#Nx.Tensor<
f32[2]
[1.0, 2.0]
>
Creates the identity matrix of given size.
Examples
iex> Qx.Math.identity(2)
#Nx.Tensor<
s32[2][2]
[
[1, 0],
[0, 1]
]
>
Computes the inner product (dot product) of two quantum states.
Examples
iex> state1 = Nx.tensor([1.0, 0.0])
iex> state2 = Nx.tensor([0.0, 1.0])
iex> Qx.Math.inner_product(state1, state2)
#Nx.Tensor<
c64
0.0+0.0i
>
Computes the Kronecker (tensor) product of two matrices.
The Kronecker product is fundamental in quantum mechanics for combining quantum states and operators across multiple qubits.
Examples
iex> a = Nx.tensor([[1, 2], [3, 4]])
iex> b = Nx.tensor([[0, 5], [6, 7]])
iex> Qx.Math.kron(a, b)
#Nx.Tensor<
s32[4][4]
[
[0, 5, 0, 10],
[6, 7, 12, 14],
[0, 15, 0, 20],
[18, 21, 24, 28]
]
>
Normalizes a quantum state vector to ensure unit magnitude.
Examples
iex> state = Nx.tensor([1.0, 1.0])
iex> Qx.Math.normalize(state)
#Nx.Tensor<
f32[2]
[0.7071067690849304, 0.7071067690849304]
>
Computes the outer product of two quantum states.
Examples
iex> state1 = Nx.tensor([1.0, 0.0])
iex> state2 = Nx.tensor([0.0, 1.0])
iex> Qx.Math.outer_product(state1, state2)
#Nx.Tensor<
c64[2][2]
[
[0.0+0.0i, 1.0+0.0i],
[0.0+0.0i, 0.0+0.0i]
]
>
Computes the probability amplitudes from a quantum state vector.
Examples
iex> state = Nx.tensor([0.7071, 0.7071])
iex> Qx.Math.probabilities(state)
#Nx.Tensor<
f32[2]
[0.4999903738498688, 0.4999903738498688]
>
Converts an Nx tensor with [real, imag] representation to a complex number.
Examples
iex> tensor = Nx.tensor([1.0, 2.0])
iex> c = Qx.Math.tensor_to_complex(tensor)
iex> Complex.real(c)
1.0
iex> Complex.imag(c)
2.0
Computes the trace of a matrix.
Examples
iex> matrix = Nx.tensor([[1.0, 2.0], [3.0, 4.0]])
iex> Qx.Math.trace(matrix)
#Nx.Tensor<
f32
5.0
>
@spec unitary?(Nx.Tensor.t()) :: boolean()
Checks if a matrix is unitary (U† U = I).
Examples
iex> pauli_x = Nx.tensor([[0.0, 1.0], [1.0, 0.0]])
iex> Qx.Math.unitary?(pauli_x)
true
iex> not_unitary = Nx.tensor([[2.0, 0.0], [0.0, 2.0]])
iex> Qx.Math.unitary?(not_unitary)
false