Skip to content

Commit

Permalink
refactor: passing state[:-1]
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Feb 8, 2024
1 parent 2897286 commit f9ea2ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
32 changes: 17 additions & 15 deletions src/qibojit/backends/clifford_operations_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def H(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (x[i, q] & z[i, q])
tmp = symplectic_matrix[i, q]
symplectic_matrix[i, q] = symplectic_matrix[i, nqubits + q]
Expand All @@ -20,7 +20,8 @@ def CNOT(symplectic_matrix, control_q, target_q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable

for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (x[i, control_q] & z[i, target_q]) & (
x[i, target_q] ^ ~z[i, control_q]
)
Expand All @@ -35,7 +36,8 @@ def CZ(symplectic_matrix, control_q, target_q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable

for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = (
r[i]
^ (x[i, target_q] & z[i, target_q])
Expand All @@ -54,7 +56,7 @@ def S(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (x[i, q] & z[i, q])
symplectic_matrix[i, nqubits + q] = z[i, q] ^ x[i, q]
return symplectic_matrix
Expand All @@ -66,7 +68,7 @@ def Z(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (
(x[i, q] & z[i, q]) ^ x[i, q] & (z[i, q] ^ x[i, q])
)
Expand All @@ -79,7 +81,7 @@ def X(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = (
r[i] ^ (z[i, q] & (z[i, q] ^ x[i, q])) ^ (z[i, q] & x[i, q])
)
Expand All @@ -92,7 +94,7 @@ def Y(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = (
r[i] ^ (z[i, q] & (z[i, q] ^ x[i, q])) ^ (x[i, q] & (z[i, q] ^ x[i, q]))
)
Expand All @@ -105,7 +107,7 @@ def SX(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (z[i, q] & (z[i, q] ^ x[i, q]))
symplectic_matrix[i, q] = z[i, q] ^ x[i, q]
return symplectic_matrix
Expand All @@ -117,7 +119,7 @@ def SDG(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (x[i, q] & (z[i, q] ^ x[i, q]))
symplectic_matrix[i, nqubits + q] = z[i, q] ^ x[i, q]
return symplectic_matrix
Expand All @@ -129,7 +131,7 @@ def SXDG(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (z[i, q] & x[i, q])
symplectic_matrix[i, q] = z[i, q] ^ x[i, q]
return symplectic_matrix
Expand All @@ -141,7 +143,7 @@ def RY_pi(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (x[i, q] & (z[i, q] ^ x[i, q]))
zq = symplectic_matrix[i, nqubits + q]
symplectic_matrix[i, nqubits + q] = symplectic_matrix[i, q]
Expand All @@ -155,7 +157,7 @@ def RY_3pi_2(symplectic_matrix, q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = r[i] ^ (z[i, q] & (z[i, q] ^ x[i, q]))
zq = symplectic_matrix[i, nqubits + q]
symplectic_matrix[i, nqubits + q] = symplectic_matrix[i, q]
Expand All @@ -169,7 +171,7 @@ def SWAP(symplectic_matrix, control_q, target_q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = (
r[i]
^ (x[i, control_q] & z[i, target_q] & (x[i, target_q] ^ ~z[i, control_q]))
Expand Down Expand Up @@ -201,7 +203,7 @@ def iSWAP(symplectic_matrix, control_q, target_q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = (
r[i]
^ (x[i, target_q] & z[i, target_q])
Expand Down Expand Up @@ -235,7 +237,7 @@ def CY(symplectic_matrix, control_q, target_q, nqubits):
r = symplectic_matrix[:-1, -1]
x = symplectic_matrix[:-1, :nqubits]
z = symplectic_matrix[:-1, nqubits:-1]
for i in prange(symplectic_matrix.shape[0] - 1): # pylint: disable=not-an-iterable
for i in prange(symplectic_matrix.shape[0]): # pylint: disable=not-an-iterable
symplectic_matrix[i, -1] = (
r[i]
^ (x[i, target_q] & (z[i, target_q] ^ x[i, target_q]))
Expand Down
15 changes: 8 additions & 7 deletions src/qibojit/backends/clifford_operations_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
GRIDDIM, BLOCKDIM = 1024, 128
GRIDDIM_2D = (1024, 1024)


@cache
def _get_dim(nqubits):
return 2 * nqubits + 1


apply_one_qubit_kernel = """
extern "C"
__global__ void apply_{}(bool* symplectic_matrix, const int q, const int nqubits, const int qz, const int dim) {{
Expand All @@ -23,14 +29,14 @@

def one_qubit_kernel_launcher(kernel, symplectic_matrix, q, nqubits):
qz = nqubits + q
dim = 2 * nqubits + 1
dim = _get_dim(nqubits)
return kernel((GRIDDIM,), (BLOCKDIM,), (symplectic_matrix, q, nqubits, qz, dim))


def two_qubits_kernel_launcher(kernel, symplectic_matrix, control_q, target_q, nqubits):
cqz = nqubits + control_q
tqz = nqubits + target_q
dim = 2 * nqubits + 1
dim = _get_dim(nqubits)
return kernel(
(GRIDDIM,),
(BLOCKDIM,),
Expand Down Expand Up @@ -623,11 +629,6 @@ def CY(symplectic_matrix, control_q, target_q, nqubits):
apply_rowsum = cp.RawKernel(apply_rowsum, "apply_rowsum", options=("--std=c++11",))


@cache
def _get_dim(nqubits):
return 2 * nqubits + 1


def _rowsum(symplectic_matrix, h, i, nqubits, determined=False):
dim = _get_dim(nqubits)
nrows = len(h)
Expand Down

0 comments on commit f9ea2ed

Please sign in to comment.