Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

radixnew.py #248

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions radixnew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Define the Radixes class

class Radixes:
def __init__(self, radixes: Union[int, Sequence[int], None], dim: int) -> None:
if isinstance(radixes, int):
self.radixes = (radixes,) * int(np.round(np.log2(dim)))
elif isinstance(radixes, Sequence):
if len(radixes) != dim:
raise ValueError(
'Length of radixes sequence does not match dimension of input matrix.'
)
self.radixes = tuple(radixes)
else:
# If radixes is not provided, infer from the dimension of the input matrix
if dim & (dim - 1) == 0:
self.radixes = tuple([2] * int(np.round(np.log2(dim))))
elif 3 ** int(np.round(np.log(dim) / np.log(3))) == dim:
self.radixes = [3] * int(np.round(np.log(dim) / np.log(3)))
else:
raise RuntimeError(
'Unable to determine radixes for UnitaryMatrix with dim %d.' % dim,
)

# Update the __init__ method in the UnitaryMatrix class

def __init__(
self,
input: UnitaryLike,
radixes: RadixesLike = None,
check_arguments: bool = True,
) -> None:
"""
Constructs a `UnitaryMatrix` from the supplied unitary matrix.

Args:
input (UnitaryLike): The unitary matrix input.

radixes (RadixesLike): An instance of Radixes class or a sequence of radices,
specifying the base, number of orthogonal states, for each qudit in the system.

check_arguments (bool): If true, check arguments for type
and value errors.

Raises:
ValueError: If `input` is not unitary.

ValueError: If the dimension of `input` does not match the
expected dimension from `radixes`.

RuntimeError: If `radixes` is not specified and the
constructor cannot infer it.
"""

# Copy constructor
if isinstance(input, UnitaryMatrix):
self._utry = input.numpy
self._radixes = input.radixes
self._dim = input.dim
return

if check_arguments and not is_square_matrix(input):
raise TypeError(f'Expected square matrix, got {type(input)}.')

if check_arguments and not UnitaryMatrix.is_unitary(input):
raise ValueError('Input failed unitary condition.')

dim = len(input)
radixes = Radixes(radixes, dim)

if check_arguments and not is_valid_radixes(radixes.radixes):
raise TypeError('Invalid qudit radixes.')

if check_arguments and np.prod(radixes.radixes) != dim:
raise ValueError('Qudit radixes mismatch with dimension.')

self._utry = np.array(input, dtype=np.complex128)
self._dim = dim
self._radixes = radixes.radixes

import unittest

class UnitaryMatrixTest(unittest.TestCase):

def test_radixes_single_radix(self):
# Test case where a single radix is provided
utry = np.array([[0, 1], [1, 0]])
unitary = UnitaryMatrix(utry, radixes=3)
self.assertEqual(unitary.radixes, (3, 3))

def test_radixes_sequence(self):
# Test case where a sequence of radices is provided
utry = np.array([[0, 1], [1, 0]])
unitary = UnitaryMatrix(utry, radixes=[2, 3])
self.assertEqual(unitary.radixes, (2, 3))

def test_radixes_infer(self):
# Test case where radixes are inferred from the dimension of the input matrix
utry = np.array([[0, 1], [1, 0], [0, 0], [0, 1]])
# Dimension of input matrix is 4, which is a power of 2
unitary = UnitaryMatrix(utry)
# Expected radixes should be [2, 2]
self.assertEqual(unitary.radixes, (2, 2))

def test_radixes_infer_non_power_of_2(self):
# Test case where dimension of input matrix is not a power of 2
utry = np.array([[0, 1], [1, 0], [0, 0], [0, 1], [1, 0]])
# Dimension of input matrix is 5, which is not a power of 2
unitary = UnitaryMatrix(utry)
# Expected radixes should be [5]
self.assertEqual(unitary.radixes, (5,))

def test_radixes_infer_non_power_of_3(self):
# Test case where dimension of input matrix is not a power of 3
utry = np.array([[0, 1], [1, 0], [0, 0], [0, 1], [1, 0], [0, 0]])
# Dimension of input matrix is 6, which is not