Skip to content

Commit

Permalink
Merge pull request #1208 from qiboteam/tensorflow
Browse files Browse the repository at this point in the history
Addition of parametrized gates missing in `TensorflowMatrices`
  • Loading branch information
renatomello authored Feb 12, 2024
2 parents 45e4e22 + 85f26bc commit 2a493f6
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/qibo/backends/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def RZ(self, theta):
phase = self.np.exp(0.5j * theta)
return self.tf.cast([[self.np.conj(phase), 0], [0, phase]], dtype=self.dtype)

def GPI(self, phi):
phase = self.np.exp(1.0j * phi)
return self.tf.cast([[0, self.np.conj(phase)], [phase, 0]], dtype=self.dtype)

def GPI2(self, phi):
phase = self.np.exp(1.0j * phi)
return self.tf.cast(
[[1, -1.0j * self.np.conj(phase)], [-1.0j * phase, 1]], dtype=self.dtype
) / self.np.sqrt(2)

def U1(self, theta):
phase = self.np.exp(1j * theta)
return self.tf.cast([[1, 0], [0, phase]], dtype=self.dtype)
Expand All @@ -59,6 +69,9 @@ def U3(self, theta, phi, lam):
dtype=self.dtype,
)

def U1q(self, theta, phi):
return self.U3(theta, phi - self.np.pi / 2, self.np.pi / 2 - phi)

def CRX(self, theta):
r = self.RX(theta)
return self.tf.cast(
Expand Down Expand Up @@ -157,6 +170,113 @@ def GeneralizedfSim(self, u, phi):
dtype=self.dtype,
)

def RXX(self, theta):
cos = self.np.cos(theta / 2.0) + 0j
isin = -1j * self.np.sin(theta / 2.0)
return self.tf.cast(
[
[cos, 0, 0, isin],
[0, cos, isin, 0],
[0, isin, cos, 0],
[isin, 0, 0, cos],
],
dtype=self.dtype,
)

def RYY(self, theta):
cos = self.np.cos(theta / 2.0) + 0j
isin = -1j * self.np.sin(theta / 2.0)
return self.tf.cast(
[
[cos, 0, 0, -isin],
[0, cos, isin, 0],
[0, isin, cos, 0],
[-isin, 0, 0, cos],
],
dtype=self.dtype,
)

def RZZ(self, theta):
phase = self.np.exp(0.5j * theta)
return self.tf.cast(
[
[self.np.conj(phase), 0, 0, 0],
[0, phase, 0, 0],
[0, 0, phase, 0],
[0, 0, 0, self.np.conj(phase)],
],
dtype=self.dtype,
)

def RZX(self, theta):
cos, sin = self.np.cos(theta / 2) + 0j, self.np.sin(theta / 2) + 0j
return self.tf.cast(
[
[cos, -1j * sin, 0, 0],
[-1j * sin, cos, 0, 0],
[0, 0, cos, 1j * sin],
[0, 0, 1j * sin, cos],
],
dtype=self.dtype,
)

def RXXYY(self, theta):
cos, sin = self.np.cos(theta / 2) + 0j, self.np.sin(theta / 2) + 0j
return self.tf.cast(
[
[1, 0, 0, 0],
[0, cos, -1j * sin, 0],
[0, -1j * sin, cos, 0],
[0, 0, 0, 1],
],
dtype=self.dtype,
)

def MS(self, phi0, phi1, theta):
plus = self.np.exp(1.0j * (phi0 + phi1))
minus = self.np.exp(1.0j * (phi0 - phi1))
cos, sin = self.np.cos(theta / 2) + 0j, self.np.sin(theta / 2) + 0j
return self.tf.cast(
[
[cos, 0, 0, -1.0j * self.np.conj(plus) * sin],
[0, cos, -1.0j * self.np.conj(minus) * sin, 0],
[0, -1.0j * minus * sin, cos, 0],
[-1.0j * plus * sin, 0, 0, cos],
],
dtype=self.dtype,
)

def GIVENS(self, theta):
return self.tf.cast(
[
[1, 0, 0, 0],
[0, self.np.cos(theta), -self.np.sin(theta), 0],
[0, self.np.sin(theta), self.np.cos(theta), 0],
[0, 0, 0, 1],
],
dtype=self.dtype,
)

def RBS(self, theta):
return self.GIVENS(-theta)

def DEUTSCH(self, theta):
sin = self.np.sin(theta) + 0j # 0j necessary for right tensorflow dtype
cos = self.np.cos(theta) + 0j
return self.tf.cast(
[
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1j * cos, sin],
[0, 0, 0, 0, 0, 0, sin, 1j * cos],
],
dtype=self.dtype,
)

def Unitary(self, u):
return self.tf.cast(u, dtype=self.dtype)

Expand Down

0 comments on commit 2a493f6

Please sign in to comment.