diff --git a/src/qureg_apply1qubitgate.cpp b/src/qureg_apply1qubitgate.cpp index 21b6b03e..7db2cc4b 100644 --- a/src/qureg_apply1qubitgate.cpp +++ b/src/qureg_apply1qubitgate.cpp @@ -461,8 +461,8 @@ void QubitRegister::ApplyHadamard(unsigned const qubit) /// /// R_XY(phi, theta) = cos(theta/2) I -i sin(theta/2) (cos(phi) X + sin(phi) Y) /// -/// = | c(t/2) -i s(t/2) (c(p) -i s(p) | -/// | -i s(t/2) (c(p) +i s(p) c(t/2) | +/// = | c(t/2) -i s(t/2) (c(p) -i s(p)) | +/// | -i s(t/2) (c(p) +i s(p)) c(t/2) | /// /// Or, in other format: /// @@ -475,7 +475,7 @@ void QubitRegister::ApplyRotationXY(unsigned const qubit, BaseType phi, Ba iqs::TinyMatrix rxy; rxy(0, 0) = Type(std::cos(theta / 2.), 0); rxy(0, 1) = Type(-std::sin(theta / 2.) * std::sin(phi), -std::sin(theta / 2.) * std::cos(phi) ); - rxy(0, 1) = Type( std::sin(theta / 2.) * std::sin(phi), -std::sin(theta / 2.) * std::cos(phi) ); + rxy(1, 0) = Type( std::sin(theta / 2.) * std::sin(phi), -std::sin(theta / 2.) * std::cos(phi) ); rxy(1, 1) = Type(std::cos(theta / 2.), 0); Apply1QubitGate(qubit, rxy); } diff --git a/unit_test/include/apply_1q_gate_test.hpp b/unit_test/include/apply_1q_gate_test.hpp index 39f65141..ac31d49c 100644 --- a/unit_test/include/apply_1q_gate_test.hpp +++ b/unit_test/include/apply_1q_gate_test.hpp @@ -203,7 +203,9 @@ TEST_F(Apply1QGateTest, RotationXY) psi.ApplyRotationZ(qubit, -phi); psi.ApplyRotationX(qubit, -theta); psi.ApplyRotationZ(qubit, phi); - ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_); + ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() > 1.-accepted_error_); // Apply rotation in the XY plane and then cancel it with two rotations. phi = 1.1; theta = 0.13; @@ -212,20 +214,23 @@ TEST_F(Apply1QGateTest, RotationXY) psi.ApplyRotationZ(qubit, -phi); psi.ApplyRotationX(qubit, -theta); psi.ApplyRotationZ(qubit, phi); - ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_); // Special case phi=0 corresponding to a X rotation. phi = 0.; theta = 0.28; qubit = 1; psi.ApplyRotationXY(qubit, phi, theta); psi.ApplyRotationX(qubit, -theta); - ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_); // Special case phi=pi/2 corresponding to a Y rotation. phi = M_PI/2; qubit = 2; psi.ApplyRotationXY(qubit, phi, theta); psi.ApplyRotationY(qubit, -theta); - ASSERT_TRUE( psi.ComputeOverlap(psi_0).real() < 1.-accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).real() - 1) < accepted_error_); + ASSERT_TRUE( std::abs(psi.ComputeOverlap(psi_0).imag() - 0) < accepted_error_); } //////////////////////////////////////////////////////////////////////////////