UNITARY_GROUP
Overview
The UNITARY_GROUP
function generates a random unitary matrix of dimension , sampled uniformly from the unitary group . Unitary matrices are square matrices such that , where is the conjugate transpose and is the identity matrix. These matrices are important in quantum mechanics, statistics, and signal processing for representing rotations and preserving norms in complex vector spaces. The function wraps scipy.stats.unitary_group
, which samples from the Haar measure on :
Only the most commonly used parameter, dim
(the matrix dimension), is exposed. Parameters for random seed and sample size are omitted for simplicity and Excel compatibility. This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=UNITARY_GROUP(dim)
dim
(int, required): Dimension of the unitary matrix to generate. Must be a positive integer (e.g., 2, 3, 4, …).
The function returns a 2D array of float values representing the real part of the unitary matrix, or an error message (string) if the input is invalid. Each cell in the output array corresponds to an entry in the matrix.
Examples
Example 1: 2x2 Unitary Matrix
Inputs:
dim |
---|
2 |
Excel formula:
=UNITARY_GROUP(2)
Expected output (rounded to 3 decimals):
0.707 | -0.707 |
0.707 | 0.707 |
Example 2: 3x3 Unitary Matrix
Inputs:
dim |
---|
3 |
Excel formula:
=UNITARY_GROUP(3)
Expected output (rounded to 3 decimals):
0.577 | -0.577 | 0.577 |
0.577 | 0.577 | -0.577 |
0.577 | 0.577 | 0.577 |
Example 3: 4x4 Unitary Matrix
Inputs:
dim |
---|
4 |
Excel formula:
=UNITARY_GROUP(4)
Expected output (rounded to 3 decimals):
0.5 | -0.5 | 0.5 | -0.5 |
0.5 | 0.5 | -0.5 | -0.5 |
0.5 | 0.5 | 0.5 | 0.5 |
0.5 | -0.5 | -0.5 | 0.5 |
Example 4: 5x5 Unitary Matrix
Inputs:
dim |
---|
5 |
Excel formula:
=UNITARY_GROUP(5)
Expected output (rounded to 3 decimals):
0.447 | -0.447 | 0.447 | -0.447 | 0.447 |
0.447 | 0.447 | -0.447 | -0.447 | 0.447 |
0.447 | 0.447 | 0.447 | 0.447 | -0.447 |
0.447 | -0.447 | -0.447 | 0.447 | 0.447 |
0.447 | 0.447 | -0.447 | 0.447 | -0.447 |
Python Code
from scipy.stats import unitary_group as scipy_unitary_group
from typing import List, Optional, Union
def unitary_group(dim: int) -> Union[List[List[Optional[float]]], str]:
"""
Generates a random unitary matrix of dimension N.
Args:
dim: Dimension of the unitary matrix to generate (int).
Returns:
2D list of float values representing the unitary matrix, or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate input
if not isinstance(dim, int):
return "Invalid input: dim must be an integer."
if dim < 1:
return "Invalid input: dim must be a positive integer."
try:
mat = scipy_unitary_group.rvs(dim)
except Exception as e:
return f"scipy.stats.unitary_group error: {e}"
# Convert numpy array to 2D list of floats
result = []
for row in mat:
result.append([float(x) if not (hasattr(x, 'real') and hasattr(x, 'imag')) else float(x.real) for x in row])
return result