Skip to content

Commit

Permalink
Fix global phase for controlled-T, R1
Browse files Browse the repository at this point in the history
The decomposition for controlled-`T` in the stdlib uses `Rz` gates that are correct up to a global phase, which is fine for hardware but distracting in simulation. Correcting for that global phase in `T` fixes the global phase in `S`, which in turn allows removing a patch that made `R1` use a global phase consistent with `S`.

Fixes #2106
  • Loading branch information
swernli committed Jan 21, 2025
1 parent 02a885e commit 69f0ce9
Show file tree
Hide file tree
Showing 3 changed files with 476 additions and 431 deletions.
49 changes: 49 additions & 0 deletions library/src/tests/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,55 @@ fn check_dumpoperation_with_extra_qubits_relative_phase_not_reflected_in_matrix(
.assert_eq(&output);
}

#[test]
fn check_dump_operation_for_r1_of_pi() {
let output = test_expression(
"Microsoft.Quantum.Diagnostics.DumpOperation(1, qs => R1(Std.Math.PI(), qs[0]))",
&Value::unit(),
);
expect![[r#"
MATRIX:
1.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 −1.0000+0.0000𝑖
"#]]
.assert_eq(&output);
}

#[test]
fn check_dump_operation_for_r1_of_pi_with_one_control() {
let output = test_expression(
"Microsoft.Quantum.Diagnostics.DumpOperation(2, qs => Controlled R1(qs[...0], (Std.Math.PI(), qs[1])))",
&Value::unit(),
);
expect![[r#"
MATRIX:
1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 −1.0000+0.0000𝑖
"#]]
.assert_eq(&output);
}

#[test]
fn check_dump_operation_for_r1_of_pi_with_two_controls() {
let output = test_expression(
"Microsoft.Quantum.Diagnostics.DumpOperation(3, qs => Controlled R1(qs[...1], (Std.Math.PI(), qs[2])))",
&Value::unit(),
);
expect![[r#"
MATRIX:
1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 1.0000+0.0000𝑖 0.0000+0.0000𝑖
0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 0.0000+0.0000𝑖 −1.0000+0.0000𝑖
"#]].assert_eq(&output);
}

#[test]
fn check_bit_flip_noise_values() {
test_expression(
Expand Down
Loading

0 comments on commit 69f0ce9

Please sign in to comment.