As noted in this post, the Amazon Braket unitary calculation method as_unitary has been deprecated (#325) as it uses little-endian qubit order. The new, big-endian method is to_unitary. Here's a code snippet showing the difference in the two forms for a simple bell circuit:
from braket.circuits import Circuit
circuit = Circuit().h(0).cnot(0, 1)
u_little_endian = circuit.as_unitary() # little-endian method
u_big_endian = circuit.to_unitary() # big-endian method
print(f"little-endian: \n{u_little_endian}\n")
print(f"big-endian: \n{u_big_endian}\n")
little-endian:
[[ 0.70710678+0.j 0.70710678+0.j 0. +0.j 0. +0.j]
[ 0. +0.j 0. +0.j 0.70710678+0.j -0.70710678+0.j]
[ 0. +0.j 0. +0.j 0.70710678+0.j 0.70710678+0.j]
[ 0.70710678+0.j -0.70710678+0.j 0. +0.j 0. +0.j]]
big-endian:
[[ 0.70710678+0.j 0. +0.j 0.70710678+0.j 0. +0.j]
[ 0. +0.j 0.70710678+0.j 0. +0.j 0.70710678+0.j]
[ 0. +0.j 0.70710678+0.j 0. +0.j -0.70710678+0.j]
[ 0.70710678+0.j 0. +0.j -0.70710678+0.j 0. +0.j]]
I'm in the process of transitioning some of my own projects to reflect this upgrade, but am also hoping to maintain backward compatibility with the little-endian method. For testing, I'm attempting to create a function that can convert a matrix calculated using the big-endian method to its little-endian "equivalent", or vice versa. For example, I'm looking to implement a function my_conversion
that would satisfy the following:
import numpy as np
u_converted = my_conversion(u_big_endian)
assert np.allclose(u_little_endian, u_converted)
In my own attempts, I've tried to work backward from the underlying functions, calculate_unitary and calculate_unitary_big_endian, however, the procedures used have been somewhat difficult to decipher. Mathematically, this post was helpful, but programmatically, I'm still not sure where to start. Maybe such a function already exists in the braket repo for internal use?