Skip to content

Commit 37e9e85

Browse files
authored
Fix RotationXY gate (#88)
* Fix RotationXY bug * Fix unit test * Add parenthesis for gate definition * Update unit tests
1 parent 453fd9b commit 37e9e85

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/qureg_apply1qubitgate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ void QubitRegister<Type>::ApplyHadamard(unsigned const qubit)
461461
///
462462
/// R_XY(phi, theta) = cos(theta/2) I -i sin(theta/2) (cos(phi) X + sin(phi) Y)
463463
///
464-
/// = | c(t/2) -i s(t/2) (c(p) -i s(p) |
465-
/// | -i s(t/2) (c(p) +i s(p) c(t/2) |
464+
/// = | c(t/2) -i s(t/2) (c(p) -i s(p)) |
465+
/// | -i s(t/2) (c(p) +i s(p)) c(t/2) |
466466
///
467467
/// Or, in other format:
468468
///
@@ -475,7 +475,7 @@ void QubitRegister<Type>::ApplyRotationXY(unsigned const qubit, BaseType phi, Ba
475475
iqs::TinyMatrix<Type, 2, 2, 32> rxy;
476476
rxy(0, 0) = Type(std::cos(theta / 2.), 0);
477477
rxy(0, 1) = Type(-std::sin(theta / 2.) * std::sin(phi), -std::sin(theta / 2.) * std::cos(phi) );
478-
rxy(0, 1) = Type( std::sin(theta / 2.) * std::sin(phi), -std::sin(theta / 2.) * std::cos(phi) );
478+
rxy(1, 0) = Type( std::sin(theta / 2.) * std::sin(phi), -std::sin(theta / 2.) * std::cos(phi) );
479479
rxy(1, 1) = Type(std::cos(theta / 2.), 0);
480480
Apply1QubitGate(qubit, rxy);
481481
}

unit_test/include/apply_1q_gate_test.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ TEST_F(Apply1QGateTest, RotationXY)
203203
psi.ApplyRotationZ(qubit, -phi);
204204
psi.ApplyRotationX(qubit, -theta);
205205
psi.ApplyRotationZ(qubit, phi);
206-
ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_);
206+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_);
207+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_);
208+
ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() > 1.-accepted_error_);
207209
// Apply rotation in the XY plane and then cancel it with two rotations.
208210
phi = 1.1;
209211
theta = 0.13;
@@ -212,20 +214,23 @@ TEST_F(Apply1QGateTest, RotationXY)
212214
psi.ApplyRotationZ(qubit, -phi);
213215
psi.ApplyRotationX(qubit, -theta);
214216
psi.ApplyRotationZ(qubit, phi);
215-
ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_);
217+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_);
218+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_);
216219
// Special case phi=0 corresponding to a X rotation.
217220
phi = 0.;
218221
theta = 0.28;
219222
qubit = 1;
220223
psi.ApplyRotationXY(qubit, phi, theta);
221224
psi.ApplyRotationX(qubit, -theta);
222-
ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_);
225+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_);
226+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_);
223227
// Special case phi=pi/2 corresponding to a Y rotation.
224228
phi = M_PI/2;
225229
qubit = 2;
226230
psi.ApplyRotationXY(qubit, phi, theta);
227231
psi.ApplyRotationY(qubit, -theta);
228-
ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_);
232+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_);
233+
ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_);
229234
}
230235

231236
//////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)