Qx.Math (Qx - Quantum Computing Simulator v0.8.0)

View Source

Core 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 complex number from real and imaginary parts.

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.

Computes the trace of a matrix.

Checks if a matrix is unitary (U† U = I).

Functions

apply_gate(gate, state)

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]
>

complex(real, imag \\ 0.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

identity(n)

Creates the identity matrix of given size.

Returns a generic n × n real-valued identity tensor (delegates to Nx.eye/1). Not gate-shaped: the 2×2 c64 single-qubit identity matrix used by gate factories is internal and is not exposed at the public surface.

Examples

iex> Qx.Math.identity(2)
#Nx.Tensor<
  s32[2][2]
  [
    [1, 0],
    [0, 1]
  ]
>

inner_product(state1, state2)

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
>

kron(a, b)

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]
  ]
>

normalize(state)

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]
>

outer_product(state1, state2)

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]
  ]
>

probabilities(state)

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]
>

trace(matrix)

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
>

unitary?(matrix)

@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