Tensor Products in Code
Tensor products become reliable numerical objects only after the order of subsystems and their basis indices is fixed. The same vector can describe different states under different orderings. This page gives an explicit mixed-radix convention and uses it to apply local operators efficiently.
Helpful background. Partial trace supplies the subsystem interpretation; array representations treats single-system storage.
Mixed-radix indexing
Section titled “Mixed-radix indexing”For subsystem dimensions , order basis states as and flatten with the last index varying fastest:
For two subsystems, . NumPy’s default C-order reshape matches this convention. Bit-based codes often make site 0 the least significant bit instead; an explicit mapping is required when exchanging data.
A product state has amplitudes . Thus a Kronecker product of statevectors reproduces this ordering. Verify the map with unequal dimensions, such as : symmetric qubit examples can conceal swapped axes.
Local operator contractions
Section titled “Local operator contractions”An operator on subsystem is formally , but constructing this full matrix is usually unnecessary. Reshape the state, move axis to the front, flatten the remaining axes, and multiply by :
Restore the axes afterward. A two-site operator uses two target axes in a documented order. Swapping their order changes a controlled gate or a nonsymmetric interaction.
import numpy as np
def local_action(psi, op, target, dims): tensor = np.asarray(psi).reshape(dims) front = np.moveaxis(tensor, target, 0) result = op @ front.reshape(dims[target], -1) result = result.reshape(front.shape) return np.moveaxis(result, 0, target).reshape(-1)This snippet assumes a square local operator preserving subsystem dimension. For an isometry with different output dimension, update both the shape and the dimension list explicitly.
A complex two-subsystem check
Section titled “A complex two-subsystem check”Let , so its flattened vector is . Applying to the first subsystem gives ; applying to the second gives .
These two expected outputs diagnose subsystem reversal. Test a complex operator such as as well: transpose and conjugation errors may survive an -only test. In a small system compare contraction output to the explicit Kronecker matrix.
An unequal-dimension check is even more discriminating. In dimensions take and . Acting on subsystem 0 must give . Acting on the qutrit with instead gives ; this deliberately nonunitary test must not be renormalized by the kernel.
For ordered targets , move those axes to the front and flatten their local index as . Multiply the rows by the specified two-site matrix, then invert the axis permutation. The targets need not be adjacent. Test both and with a nonsymmetric gate: the same numerical matrix represents different physical operators under those two interpretations. Reshaping without the required permutation does not relocate an operator.
Cost and physical restrictions
Section titled “Cost and physical restrictions”For total dimension , a fixed-size local action costs up to local dimension factors and needs state storage. A dense global matrix would require memory. Axis permutations can create copies, so peak memory depends on array layout and library behavior.
A tensor factorization is not automatically a factorization into distinguishable physical particles. Fermionic mode operators need parity signs; applying bare local raising and lowering matrices on a qubit tensor product does not implement the canonical anticommutation relations. Use fermionic basis construction for that case.
Finally, reduced symmetry-sector vectors are not full tensor arrays. Embed them through their basis map before a generic reshape or implement contractions directly in the reduced basis.
Exercise
Section titled “Exercise”In dimensions , find the flat index of and the indices changed by a local on the final subsystem.
Solution
. The final bit toggles and while leaving the first two indices unchanged. A bitwise toggle of the most significant bit would instead act on a different subsystem.
References
Section titled “References”- QuTiP developers, “Tensor Products and Partial Traces”, QuTiP documentation — subsystem dimensions and operator placement.
- Watrous, J., The Theory of Quantum Information, Cambridge University Press (2018), author’s text — tensor spaces and linear maps.