Skip to content

Commit 33dbf24

Browse files
committed
Refactor coherent state implementation
Signed-off-by: Thien Nguyen <[email protected]>
1 parent 35227c2 commit 33dbf24

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

python/cudaq/operator/cudm_state.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,21 @@ def ket2dm(ket: cupy.ndarray) -> cupy.ndarray:
3737

3838
# Helper to create a 'coherent' state as a state vector.
3939
def coherent_state(N: int, alpha: float):
40-
sqrtn = cupy.sqrt(cupy.arange(0, N, dtype=cupy.complex128))
41-
sqrtn[0] = 1
42-
data = alpha / sqrtn
43-
data[0] = cupy.exp(-cupy.abs(alpha)**2 / 2.0)
44-
cupy.cumprod(data, out=sqrtn)
45-
return sqrtn
40+
# Computes coherent state amplitudes in the Fock basis
41+
# Ref: https://en.wikipedia.org/wiki/Coherent_state
42+
fock_amplitudes = cupy.zeros(N, dtype=cupy.complex128)
43+
amplitude = numpy.exp(-numpy.abs(alpha)**2 / 2.0)
44+
for n in range(N):
45+
fock_amplitudes[n] = amplitude
46+
amplitude *= (alpha / numpy.sqrt(n + 1))
47+
return fock_amplitudes
4648

4749

4850
# Helper to create a coherent state as a density matrix.
4951
def coherent_dm(N: int, alpha: float):
5052
return ket2dm(coherent_state(N, alpha))
5153

54+
5255
# A Python wrapper of `CuDensityMatState` state.
5356
class CuDensityMatState(object):
5457
__ctx = None

0 commit comments

Comments
 (0)