Skip to content

Commit

Permalink
Reduce use of const values PI2 and PI4 for pi/2 and pi/4
Browse files Browse the repository at this point in the history
Before this batch of commits (PR), `PI2` and `PI4` were defined and used in
a few files.

With this commit:

In files with few uses of `PI2` and `PI4`, the uses are replaced by the explicit
expressions `PI / 2.` and `PI / 4.`.

In one file where `PI2` and `PI4` are used heavily, we preserve the file-local definitions.
  • Loading branch information
jlapeyre committed Jun 27, 2024
1 parent 44df001 commit b6b315e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
6 changes: 3 additions & 3 deletions crates/accelerate/src/two_qubit_decompose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ use rand_distr::StandardNormal;
use rand_pcg::Pcg64Mcg;

use qiskit_circuit::gate_matrix::{CX_GATE, H_GATE, ONE_QUBIT_IDENTITY, SX_GATE, X_GATE};
use qiskit_circuit::util::{
c64, GateArray1Q, GateArray2Q, C_M_ONE, C_ONE, C_ZERO, IM, M_IM, PI2, PI4,
};
use qiskit_circuit::util::{c64, GateArray1Q, GateArray2Q, C_M_ONE, C_ONE, C_ZERO, IM, M_IM};
use qiskit_circuit::SliceOrInt;

const PI2: f64 = PI / 2.;
const PI4: f64 = PI / 4.;
const PI32: f64 = 3.0 * PI2;
const TWO_PI: f64 = 2.0 * PI;

Expand Down
6 changes: 3 additions & 3 deletions crates/accelerate/src/uc_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use ndarray::prelude::*;
use numpy::{IntoPyArray, PyReadonlyArray2};

use crate::euler_one_qubit_decomposer::det_one_qubit;
use qiskit_circuit::util::{c64, C_ZERO, IM, PI2};
use qiskit_circuit::util::{c64, C_ZERO, IM};

const EPS: f64 = 1e-10;

Expand All @@ -48,8 +48,8 @@ fn demultiplex_single_uc(
let x11 = x[[0, 0]] / det_x.sqrt();
let phi = det_x.arg();

let r1 = (IM / 2. * (PI2 - phi / 2. - x11.arg())).exp();
let r2 = (IM / 2. * (PI2 - phi / 2. + x11.arg() + PI)).exp();
let r1 = (IM / 2. * (PI / 2. - phi / 2. - x11.arg())).exp();
let r2 = (IM / 2. * (PI / 2. - phi / 2. + x11.arg() + PI)).exp();

let r = array![[r1, C_ZERO], [C_ZERO, r2],];

Expand Down
33 changes: 18 additions & 15 deletions crates/circuit/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::f64::consts::PI;

use crate::circuit_data::CircuitData;
use crate::imports::{PARAMETER_EXPRESSION, QUANTUM_CIRCUIT};
use crate::util::{PI2, PI4};
use crate::{gate_matrix, Qubit};

use ndarray::{aview2, Array2};
Expand Down Expand Up @@ -553,7 +552,11 @@ impl Operation for StandardGate {
1,
[(
Self::UGate,
smallvec![Param::Float(PI), Param::Float(PI2), Param::Float(PI2),],
smallvec![
Param::Float(PI),
Param::Float(PI / 2.),
Param::Float(PI / 2.),
],
smallvec![Qubit(0)],
)],
FLOAT_ZERO,
Expand Down Expand Up @@ -722,7 +725,7 @@ impl Operation for StandardGate {
1,
[(
Self::UGate,
smallvec![Param::Float(PI2), Param::Float(0.), Param::Float(PI)],
smallvec![Param::Float(PI / 2.), Param::Float(0.), Param::Float(PI)],
smallvec![Qubit(0)],
)],
FLOAT_ZERO,
Expand Down Expand Up @@ -753,7 +756,7 @@ impl Operation for StandardGate {
1,
[(
Self::PhaseGate,
smallvec![Param::Float(PI2)],
smallvec![Param::Float(PI / 2.)],
smallvec![Qubit(0)],
)],
FLOAT_ZERO,
Expand Down Expand Up @@ -783,7 +786,7 @@ impl Operation for StandardGate {
1,
[(
Self::PhaseGate,
smallvec![Param::Float(-PI2)],
smallvec![Param::Float(-PI / 2.)],
smallvec![Qubit(0)],
)],
FLOAT_ZERO,
Expand Down Expand Up @@ -813,7 +816,7 @@ impl Operation for StandardGate {
1,
[(
Self::PhaseGate,
smallvec![Param::Float(PI4)],
smallvec![Param::Float(PI / 4.)],
smallvec![Qubit(0)],
)],
FLOAT_ZERO,
Expand Down Expand Up @@ -843,7 +846,7 @@ impl Operation for StandardGate {
1,
[(
Self::PhaseGate,
smallvec![Param::Float(-PI4)],
smallvec![Param::Float(-PI / 4.)],
smallvec![Qubit(0)],
)],
FLOAT_ZERO,
Expand Down Expand Up @@ -885,9 +888,9 @@ impl Operation for StandardGate {
smallvec![multiply_param(beta, -1.0, py)],
q1.clone(),
),
(Self::RZGate, smallvec![Param::Float(-PI2)], q0.clone()),
(Self::RZGate, smallvec![Param::Float(-PI / 2.)], q0.clone()),
(Self::SXGate, smallvec![], q0.clone()),
(Self::RZGate, smallvec![Param::Float(PI2)], q0.clone()),
(Self::RZGate, smallvec![Param::Float(PI / 2.)], q0.clone()),
(Self::SGate, smallvec![], q1.clone()),
(Self::CXGate, smallvec![], q0_1.clone()),
(
Expand All @@ -902,9 +905,9 @@ impl Operation for StandardGate {
),
(Self::CXGate, smallvec![], q0_1),
(Self::SdgGate, smallvec![], q1.clone()),
(Self::RZGate, smallvec![Param::Float(-PI2)], q0.clone()),
(Self::RZGate, smallvec![Param::Float(-PI / 2.)], q0.clone()),
(Self::SXdgGate, smallvec![], q0.clone()),
(Self::RZGate, smallvec![Param::Float(PI2)], q0),
(Self::RZGate, smallvec![Param::Float(PI / 2.)], q0),
(Self::RZGate, smallvec![beta.clone()], q1),
],
FLOAT_ZERO,
Expand All @@ -924,9 +927,9 @@ impl Operation for StandardGate {
2,
[
(Self::RZGate, smallvec![beta.clone()], q0.clone()),
(Self::RZGate, smallvec![Param::Float(-PI2)], q1.clone()),
(Self::RZGate, smallvec![Param::Float(-PI / 2.)], q1.clone()),
(Self::SXGate, smallvec![], q1.clone()),
(Self::RZGate, smallvec![Param::Float(PI2)], q1.clone()),
(Self::RZGate, smallvec![Param::Float(PI / 2.)], q1.clone()),
(Self::SGate, smallvec![], q0.clone()),
(Self::CXGate, smallvec![], q1_0.clone()),
(
Expand All @@ -941,9 +944,9 @@ impl Operation for StandardGate {
),
(Self::CXGate, smallvec![], q1_0),
(Self::SdgGate, smallvec![], q0.clone()),
(Self::RZGate, smallvec![Param::Float(-PI2)], q1.clone()),
(Self::RZGate, smallvec![Param::Float(-PI / 2.)], q1.clone()),
(Self::SXdgGate, smallvec![], q1.clone()),
(Self::RZGate, smallvec![Param::Float(PI2)], q1),
(Self::RZGate, smallvec![Param::Float(PI / 2.)], q1),
(Self::RZGate, smallvec![multiply_param(beta, -1.0, py)], q0),
],
FLOAT_ZERO,
Expand Down
4 changes: 0 additions & 4 deletions crates/circuit/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// that they have been altered from the originals.

use num_complex::Complex64;
use std::f64::consts::PI;

// This is a very conservative version of an abbreviation for constructing new Complex64.
// A couple of alternatives to this function are
Expand Down Expand Up @@ -47,6 +46,3 @@ pub const C_ONE: Complex64 = c64(1., 0.);
pub const C_M_ONE: Complex64 = c64(-1., 0.);
pub const IM: Complex64 = c64(0., 1.);
pub const M_IM: Complex64 = c64(0., -1.);

pub const PI2: f64 = PI / 2.0;
pub const PI4: f64 = PI / 4.0;

0 comments on commit b6b315e

Please sign in to comment.