Skip to content

Commit

Permalink
Cast unitaries to tf
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros11 committed Mar 25, 2021
1 parent c2dfeff commit 71dc2f6
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/qibo/core/cgates.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def __init__(self, *q):
gates.I.__init__(self, *q)

def construct_unitary(self):
return K.qnp.eye(2 ** len(self.target_qubits))
return K.eye(2 ** len(self.target_qubits))

def state_vector_call(self, state):
return state
Expand Down Expand Up @@ -312,8 +312,8 @@ def __init__(self, q, theta, trainable=True):

def construct_unitary(self):
theta = self.parameters
cos, isin = K.qnp.cos(theta / 2.0), -1j * K.qnp.sin(theta / 2.0)
return K.qnp.cast([[cos, isin], [isin, cos]])
cos, isin = K.qnp.cos(theta / 2.0) + 0j, -1j * K.qnp.sin(theta / 2.0)
return K.cast([[cos, isin], [isin, cos]])


class RY(MatrixGate, gates.RY):
Expand All @@ -325,7 +325,7 @@ def __init__(self, q, theta, trainable=True):
def construct_unitary(self):
theta = self.parameters
cos, sin = K.qnp.cos(theta / 2.0), K.qnp.sin(theta / 2.0)
return K.qnp.cast([[cos, -sin], [sin, cos]])
return K.cast([[cos, -sin], [sin, cos]])


class RZ(MatrixGate, gates.RZ):
Expand All @@ -336,7 +336,7 @@ def __init__(self, q, theta, trainable=True):

def construct_unitary(self):
phase = K.qnp.exp(1j * self.parameters / 2.0)
return K.qnp.diag([K.qnp.conj(phase), phase])
return K.cast(K.qnp.diag([K.qnp.conj(phase), phase]))


class U1(MatrixGate, gates.U1):
Expand All @@ -347,9 +347,7 @@ def __init__(self, q, theta, trainable=True):
self.gate_op = K.op.apply_z_pow

def reprepare(self):
with K.device(self.device):
self.matrix = K.cast(K.qnp.exp(1j * self.parameters),
dtype='DTYPECPX')
self.matrix = K.cast(K.qnp.exp(1j * self.parameters))

def construct_unitary(self):
return K.qnp.diag([1, K.qnp.exp(1j * self.parameters)])
Expand All @@ -365,8 +363,8 @@ def construct_unitary(self):
phi, lam = self.parameters
eplus = K.qnp.exp(1j * (phi + lam) / 2.0)
eminus = K.qnp.exp(1j * (phi - lam) / 2.0)
return K.qnp.cast([[eplus.conj(), - eminus.conj()],
[eminus, eplus]]) / K.qnp.sqrt(2)
return K.cast([[eplus.conj(), - eminus.conj()],
[eminus, eplus]]) / K.qnp.sqrt(2)


class U3(MatrixGate, gates.U3):
Expand All @@ -381,8 +379,8 @@ def construct_unitary(self):
sint = K.qnp.sin(theta / 2)
eplus = K.qnp.exp(1j * (phi + lam) / 2.0)
eminus = K.qnp.exp(1j * (phi - lam) / 2.0)
return K.qnp.cast([[eplus.conj() * cost, - eminus.conj() * sint],
[eminus * sint, eplus * cost]])
return K.cast([[eplus.conj() * cost, - eminus.conj() * sint],
[eminus * sint, eplus * cost]])


class ZPow(gates.ZPow):
Expand Down Expand Up @@ -509,11 +507,9 @@ def __init__(self, q0, q1, theta, phi, trainable=True):

def reprepare(self):
theta, phi = self.parameters
cos, isin = K.qnp.cos(theta), -1j * K.qnp.sin(theta)
cos, isin = K.qnp.cos(theta) + 0j, -1j * K.qnp.sin(theta)
phase = K.qnp.exp(-1j * phi)
matrix = K.qnp.cast([cos, isin, isin, cos, phase])
with K.device(self.device):
self.matrix = K.cast(matrix)
self.matrix = K.cast([cos, isin, isin, cos, phase])

def construct_unitary(self):
theta, phi = self.parameters
Expand All @@ -522,7 +518,7 @@ def construct_unitary(self):
matrix[1, 1], matrix[2, 2] = cos, cos
matrix[1, 2], matrix[2, 1] = isin, isin
matrix[3, 3] = K.qnp.exp(-1j * phi)
return matrix
return K.cast(matrix)


class GeneralizedfSim(MatrixGate, gates.GeneralizedfSim):
Expand All @@ -537,15 +533,14 @@ def reprepare(self):
matrix = K.qnp.zeros(5)
matrix[:4] = K.qnp.reshape(unitary, (4,))
matrix[4] = K.qnp.exp(-1j * phi)
with K.device(self.device):
self.matrix = K.cast(matrix)
self.matrix = K.cast(matrix)

def construct_unitary(self):
unitary, phi = self.parameters
matrix = K.qnp.eye(4)
matrix[1:3, 1:3] = K.qnp.reshape(unitary, (2, 2))
matrix[3, 3] = K.qnp.exp(-1j * phi)
return matrix
return K.cast(matrix)

def _dagger(self) -> "GenerelizedfSim":
unitary, phi = self.parameters
Expand Down Expand Up @@ -1052,10 +1047,10 @@ def target_qubits_dm(self):

def construct_unitary(self):
matrix = K.qnp.diag([1 - self.preset1, self.exp_t2, self.exp_t2,
1 - self.preset0])
1 - self.preset0])
matrix[0, -1] = self.preset1
matrix[-1, 0] = self.preset0
return matrix
return K.cast(matrix)

def state_vector_call(self, state):
raise_error(ValueError, "Thermal relaxation cannot be applied to "
Expand Down

0 comments on commit 71dc2f6

Please sign in to comment.