Skip to content
Merged
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
13 changes: 10 additions & 3 deletions python/quantum-pecos/src/pecos/simulators/custatevec/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, num_qubits: int, seed: int | None = None) -> None:
num_qubits (int): Number of qubits being represented.
seed (int): Seed for randomness.
"""
self.libhandle = None
if not isinstance(num_qubits, int):
msg = "``num_qubits`` should be of type ``int``."
raise TypeError(msg)
Expand All @@ -59,6 +60,7 @@ def __init__(self, num_qubits: int, seed: int | None = None) -> None:
self.compute_type = ComputeType.COMPUTE_64F

# Allocate the statevector in GPU and initialize it to |0>
self.cupy_vector = None
self.reset()

####################################################
Expand Down Expand Up @@ -106,15 +108,20 @@ def free(ptr: int, _size: int, stream: Any) -> None: # noqa: ANN401
def reset(self) -> Self:
"""Reset the quantum state for another run without reinitializing."""
# Initialize all qubits in the zero state
self.cupy_vector = cp.zeros(shape=2**self.num_qubits, dtype=self.cp_type)
self.cupy_vector[0] = 1
if self.cupy_vector is not None:
self.cupy_vector[:] = 0
self.cupy_vector[0] = 1
else:
self.cupy_vector = cp.zeros(shape=2**self.num_qubits, dtype=self.cp_type)
self.cupy_vector[0] = 1
return self

def __del__(self) -> None:
"""Clean up GPU resources when the object is destroyed."""
# CuPy will release GPU memory when the variable ``self.cupy_vector`` is no longer
# reachable. However, we need to manually destroy the library handle.
cusv.destroy(self.libhandle)
if self.libhandle:
cusv.destroy(self.libhandle)

@property
def vector(self) -> ArrayLike:
Expand Down
Loading