Skip to content

Commit

Permalink
Merge branch 'justink/mcr_gates' into justink/qsd_decomp
Browse files Browse the repository at this point in the history
  • Loading branch information
jkalloor3 committed Sep 27, 2024
2 parents fbf5baf + 0d54591 commit e812db7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
22 changes: 11 additions & 11 deletions bqskit/ir/gates/parameterized/mpry.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ def get_grad(self, params: RealVector = []) -> npt.NDArray[np.complex128]:
"""
self.check_parameters(params)

orig_utry = self.get_unitary(params).numpy
grad = []
grad = np.zeros(
(
len(params), 2 ** self.num_qudits,
2 ** self.num_qudits,
), dtype=np.complex128,
)

# For each parameter, calculate the derivative
# with respect to that parameter
Expand All @@ -117,16 +121,12 @@ def get_grad(self, params: RealVector = []) -> npt.NDArray[np.complex128]:
# Again, get indices based on target qubit.
x1, x2 = get_indices(i, self.target_qubit, self.num_qudits)

matrix = orig_utry.copy()

matrix[x1, x1] = dcos
matrix[x2, x2] = dcos
matrix[x2, x1] = dsin
matrix[x1, x2] = -1 * dsin

grad.append(matrix)
grad[i, x1, x1] = dcos
grad[i, x2, x2] = dcos
grad[i, x2, x1] = dsin
grad[i, x1, x2] = -1 * dsin

return np.array(grad)
return grad

def optimize(self, env_matrix: npt.NDArray[np.complex128]) -> list[float]:
"""
Expand Down
25 changes: 12 additions & 13 deletions bqskit/ir/gates/parameterized/mprz.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,27 @@ def get_grad(self, params: RealVector = []) -> npt.NDArray[np.complex128]:
See :class:`DifferentiableUnitary` for more info.
"""
self.check_parameters(params)
orig_utry = self.get_unitary(params).numpy
grad = []

grad = np.zeros(
(
len(params), 2 ** self.num_qudits,
2 ** self.num_qudits,
), dtype=np.complex128,
)

# For each parameter, calculate the derivative
# with respect to that parameter
for i, param in enumerate(params):
dcos = -np.sin(param / 2) / 2
dsin = -1j * np.cos(param / 2) / 2
dpos = 1j * np.exp(1j * param / 2) / 2
dneg = -1j * np.exp(-1j * param / 2) / 2

# Again, get indices based on target qubit.
x1, x2 = get_indices(i, self.target_qubit, self.num_qudits)

matrix = orig_utry.copy()

matrix[x1, x1] = dcos
matrix[x2, x2] = dcos
matrix[x2, x1] = dsin
matrix[x1, x2] = -1 * dsin

grad.append(matrix)
grad[i, x1, x1] = dpos
grad[i, x2, x2] = dneg

return np.array(grad)
return grad

def optimize(self, env_matrix: npt.NDArray[np.complex128]) -> list[float]:
"""
Expand Down

0 comments on commit e812db7

Please sign in to comment.