Skip to content

Commit cb3dc89

Browse files
authored
Merge pull request #109 from qiboteam/identity_density_matrix
Identity density matrix
2 parents d92276a + fea58e0 commit cb3dc89

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/qibojit/backends/gpu.py

+9
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ def zero_density_matrix(self, nqubits):
156156
self.cp.cuda.stream.get_current_stream().synchronize()
157157
return state.reshape((n, n))
158158

159+
def identity_density_matrix(self, nqubits):
160+
n = 1 << nqubits
161+
kernel = self.gates.get(f"initial_state_kernel_{self.kernel_type}")
162+
state = self.cp.eye(n, dtype=self.dtype)
163+
kernel((1,), (1,), [state])
164+
self.cp.cuda.stream.get_current_stream().synchronize()
165+
state /= 2**nqubits
166+
return state.reshape((n, n))
167+
159168
def plus_state(self, nqubits):
160169
state = self.cp.ones(2**nqubits, dtype=self.dtype)
161170
state /= self.cp.sqrt(2**nqubits)

src/qibojit/backends/matrices.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import sys
2+
from functools import cached_property # pylint: disable=E0611
23

34
import numpy as np
45
from qibo.backends.npmatrices import NumpyMatrices
56

6-
if sys.version_info.minor >= 8:
7-
from functools import cached_property # pylint: disable=E0611
8-
else:
9-
# Custom ``cached_property`` because it is not available for Python < 3.8
10-
from functools import lru_cache
11-
12-
def cached_property(func): # pragma: no cover
13-
@property
14-
def wrapper(self):
15-
return lru_cache()(func)(self)
16-
17-
return wrapper
18-
197

208
class CuQuantumMatrices(NumpyMatrices):
219
# These matrices are used by the custom operators and may

src/qibojit/tests/test_ops.py

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def test_zero_state(backend, dtype, is_matrix):
2020
backend.assert_allclose(final_state, target_state)
2121

2222

23+
def test_identity_density_matrix(backend, dtype):
24+
set_precision(dtype, backend)
25+
final_state = backend.identity_density_matrix(4)
26+
target_state = np.eye(16, 16, dtype=dtype) / 16
27+
backend.assert_allclose(final_state, target_state)
28+
29+
2330
@pytest.mark.parametrize("is_matrix", [False, True])
2431
def test_plus_state(backend, dtype, is_matrix):
2532
set_precision(dtype, backend)

0 commit comments

Comments
 (0)