Skip to content

Commit

Permalink
Fix CRot
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Jun 4, 2024
1 parent ec4b63d commit c11e871
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
25 changes: 14 additions & 11 deletions pennylane_qrack/qrack_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,19 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
qsim->RZ(inverse ? -params[0U] : params[0U], target);
}
} else if (name == "Rot") {
const Qrack::real1 phi = inverse ? -params[2U] : params[0U];
const Qrack::real1 theta = inverse ? -params[1U] : params[1U];
const Qrack::real1 omega = inverse ? -params[0U] : params[2U];
const Qrack::real1 cos0 = (Qrack::real1)cos(theta / 2);
const Qrack::real1 sin0 = (Qrack::real1)sin(theta / 2);
const Qrack::complex expP = exp(Qrack::I_CMPLX * (phi + omega) / (2 * ONE_R1));
const Qrack::complex expM = exp(Qrack::I_CMPLX * (phi - omega) / (2 * ONE_R1));
const Qrack::complex mtrx[4U]{
cos0 / expP, -sin0 * expM,
sin0 / expM, cos0 * expP
};
for (const bitLenInt& target : wires) {
if (inverse) {
qsim->RZ(-params[2U], target);
qsim->RY(-params[1U], target);
qsim->RZ(-params[0U], target);
} else {
qsim->RZ(params[0U], target);
qsim->RY(params[1U], target);
qsim->RZ(params[2U], target);
}
qsim->Mtrx(mtrx, target);
}
} else if (name == "U3") {
for (const bitLenInt& target : wires) {
Expand Down Expand Up @@ -328,9 +331,9 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
qsim->UCPhase(control_wires, conj(bottomRight), bottomRight, target, controlPerm);
}
} else if ((name == "Rot") || (name == "CRot")) {
const Qrack::real1 phi = inverse ? -params[0U] : params[0U];
const Qrack::real1 phi = inverse ? -params[2U] : params[0U];
const Qrack::real1 theta = inverse ? -params[1U] : params[1U];
const Qrack::real1 omega = inverse ? -params[2U] : params[2U];
const Qrack::real1 omega = inverse ? -params[0U] : params[2U];
const Qrack::real1 cos0 = (Qrack::real1)cos(theta / 2);
const Qrack::real1 sin0 = (Qrack::real1)sin(theta / 2);
const Qrack::complex expP = exp(Qrack::I_CMPLX * (phi + omega) / (2 * ONE_R1));
Expand Down
7 changes: 4 additions & 3 deletions pennylane_qrack/qrack_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,17 @@ def _apply_gate(self, op):
theta = par[1]
omega = par[2]
if ".inv" in opname:
phi = -phi
tmp = phi
phi = -omega
theta = -theta
omega = -omega
omega = -phi
c = math.cos(theta / 2)
s = math.sin(theta / 2)
mtrx = [
cmath.exp(-0.5j * (phi + omega)) * c,
cmath.exp(0.5j * (phi - omega)) * s,
cmath.exp(-0.5j * (phi - omega)) * s,
cmath.exp(0.5j * (phi + omega)) * c,
cmath.exp(0.5j * (phi + omega)) * np.cos(theta / 2),
]
self._state.mcmtrx(device_wires.labels[:-1], mtrx, device_wires.labels[-1])
elif opname in ["SWAP", "SWAP.inv"]:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
(qml.CRot(0, 0, 0, wires=[0, 1]), crot),
(
qml.adjoint(qml.CRot(0, 0, 0, wires=[0, 1])),
lambda phi, theta, omega: crot(-phi, -theta, -omega),
lambda phi, theta, omega: crot(-omega, -theta, -phi),
),
]
# list of all three-qubit gates
Expand Down Expand Up @@ -579,7 +579,9 @@ def test_two_qubit_three_parameters(
dev._obs_queue = []

res = dev.state
res = [(x * np.conjugate(x)).real for x in res]
expected = func(phi, theta, omega) @ state
expected = [(x * x.conj()).real for x in expected]
assert np.allclose(res, expected, tol)

def test_apply_errors_qubit_state_vector(self):
Expand Down

0 comments on commit c11e871

Please sign in to comment.