Skip to content

Commit da47ded

Browse files
authored
Concise StandardGate::<ID> enums in Rust space (Qiskit#14026)
* Rename `StandardGate::([A-Z])*Gate --> StandardGate::$1` * patch comments * patch more comments * `cargo fmt` * fix rust class ref & full qualifiers in CommCancel
1 parent 219dec7 commit da47ded

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1013
-1196
lines changed

crates/accelerate/src/circuit_library/iqp.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,33 @@ fn iqp(
3636

3737
// The initial and final Hadamard layer.
3838
let h_layer =
39-
(0..num_qubits).map(|i| (StandardGate::HGate, smallvec![], smallvec![Qubit(i as u32)]));
39+
(0..num_qubits).map(|i| (StandardGate::H, smallvec![], smallvec![Qubit(i as u32)]));
4040

41-
// The circuit interactions are powers of the CSGate, which is implemented by calling
42-
// the CPhaseGate with angles of Pi/2 times the power. The gate powers are given by the
41+
// The circuit interactions are powers of the CS gate, which is implemented by calling
42+
// the CPhase gate with angles of Pi/2 times the power. The gate powers are given by the
4343
// upper triangular part of the symmetric ``interactions`` matrix.
4444
let connections = (0..num_qubits).flat_map(move |i| {
4545
(i + 1..num_qubits)
4646
.map(move |j| (j, interactions[(i, j)]))
4747
.filter(move |(_, value)| value % 4 != 0)
4848
.map(move |(j, value)| {
4949
(
50-
StandardGate::CPhaseGate,
50+
StandardGate::CPhase,
5151
smallvec![Param::Float(PI2 * value as f64)],
5252
smallvec![Qubit(i as u32), Qubit(j as u32)],
5353
)
5454
})
5555
});
5656

57-
// The layer of T gates. Again we use the PhaseGate, now with powers of Pi/8. The powers
57+
// The layer of T gates. Again we use the Phase gate, now with powers of Pi/8. The powers
5858
// are given by the diagonal of the ``interactions`` matrix.
5959
let shifts = (0..num_qubits)
6060
.map(move |i| interactions[(i, i)])
6161
.enumerate()
6262
.filter(|(_, value)| value % 8 != 0)
6363
.map(|(i, value)| {
6464
(
65-
StandardGate::PhaseGate,
65+
StandardGate::Phase,
6666
smallvec![Param::Float(PI8 * value as f64)],
6767
smallvec![Qubit(i as u32)],
6868
)

crates/accelerate/src/circuit_library/pauli_evolution.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type Instruction = (
3737
/// time: The rotation angle. Note that this will directly be used as input of the
3838
/// rotation gate and not be multiplied by a factor of 2 (that should be done before so
3939
/// that this function can remain Rust-only).
40-
/// phase_gate_for_paulis: If ``true``, use the ``PhaseGate`` instead of ``RZGate`` as
41-
/// single-qubit Pauli rotations.
40+
/// phase_gate_for_paulis: If ``true``, use the [StandardGate::Phase] instead of
41+
/// [StandardGate::RZ] single-qubit Pauli rotations.
4242
/// do_fountain: If ``true``, implement the CX propagation as "fountain" shape, where each
4343
/// CX uses the top qubit as target. If ``false``, uses a "chain" shape, where CX in between
4444
/// neighboring qubits are used.
@@ -85,23 +85,23 @@ fn single_qubit_evolution(
8585
) -> Box<dyn Iterator<Item = Instruction>> {
8686
let qubit = vec![Qubit(index)];
8787

88-
// We don't need to explictly cover the |1><1| projector case (which is the PhaseGate),
88+
// We don't need to explictly cover the |1><1| projector case (which is the Phase gate),
8989
// which will be handled by the multi-qubit evolution.
9090
match pauli {
9191
'x' => Box::new(std::iter::once((
92-
StandardGate::RXGate.into(),
92+
StandardGate::RX.into(),
9393
smallvec![time],
9494
qubit,
9595
vec![],
9696
))),
9797
'y' => Box::new(std::iter::once((
98-
StandardGate::RYGate.into(),
98+
StandardGate::RY.into(),
9999
smallvec![time],
100100
qubit,
101101
vec![],
102102
))),
103103
'z' => Box::new(std::iter::once((
104-
StandardGate::RZGate.into(),
104+
StandardGate::RZ.into(),
105105
smallvec![time],
106106
qubit,
107107
vec![],
@@ -130,36 +130,36 @@ fn two_qubit_evolution<'a>(
130130
let qubits = vec![Qubit(indices[0]), Qubit(indices[1])];
131131
let paulistring: String = pauli.iter().collect();
132132

133-
// We don't need to explictly cover the |11><11| projector case (which is the CPhaseGate),
133+
// We don't need to explictly cover the |11><11| projector case (which is the CPhase gate),
134134
// which will be handled by the multi-qubit evolution. The Paulis need special treatment here
135135
// since the generic code would use CX-RZ-CX instead of the two-qubit Pauli standard gates.
136136
match paulistring.as_str() {
137137
"xx" => Box::new(std::iter::once((
138-
StandardGate::RXXGate.into(),
138+
StandardGate::RXX.into(),
139139
smallvec![time],
140140
qubits,
141141
vec![],
142142
))),
143143
"zx" => Box::new(std::iter::once((
144-
StandardGate::RZXGate.into(),
144+
StandardGate::RZX.into(),
145145
smallvec![time],
146146
qubits,
147147
vec![],
148148
))),
149149
"xz" => Box::new(std::iter::once((
150-
StandardGate::RZXGate.into(),
150+
StandardGate::RZX.into(),
151151
smallvec![time],
152152
vec![qubits[1], qubits[0]],
153153
vec![],
154154
))),
155155
"yy" => Box::new(std::iter::once((
156-
StandardGate::RYYGate.into(),
156+
StandardGate::RYY.into(),
157157
smallvec![time],
158158
qubits,
159159
vec![],
160160
))),
161161
"zz" => Box::new(std::iter::once((
162-
StandardGate::RZZGate.into(),
162+
StandardGate::RZZ.into(),
163163
smallvec![time],
164164
qubits,
165165
vec![],
@@ -194,13 +194,13 @@ fn multi_qubit_evolution(
194194
let q = Qubit(*index);
195195
match bit_term {
196196
'x' | '+' | '-' => basis_change.push((
197-
StandardGate::HGate.into(),
197+
StandardGate::H.into(),
198198
smallvec![],
199199
vec![q],
200200
empty_clbits.clone(),
201201
)),
202202
'y' | 'r' | 'l' => basis_change.push((
203-
StandardGate::SXGate.into(),
203+
StandardGate::SX.into(),
204204
smallvec![],
205205
vec![q],
206206
empty_clbits.clone(),
@@ -220,14 +220,14 @@ fn multi_qubit_evolution(
220220
let inverse_basis_change: Vec<Instruction> = basis_change
221221
.iter()
222222
.map(|(gate, _, qubit, _)| match gate.standard_gate() {
223-
StandardGate::HGate => (
224-
StandardGate::HGate.into(),
223+
StandardGate::H => (
224+
StandardGate::H.into(),
225225
smallvec![],
226226
qubit.clone(),
227227
empty_clbits.clone(),
228228
),
229-
StandardGate::SXGate => (
230-
StandardGate::SXdgGate.into(),
229+
StandardGate::SX => (
230+
StandardGate::SXdg.into(),
231231
smallvec![],
232232
qubit.clone(),
233233
empty_clbits.clone(),
@@ -255,9 +255,9 @@ fn multi_qubit_evolution(
255255
// per default (unless the user specified otherwise)
256256
let params: SmallVec<[Param; 3]> = smallvec![time];
257257
let base_gate = if phase_gate_for_paulis {
258-
StandardGate::PhaseGate
258+
StandardGate::Phase
259259
} else {
260-
StandardGate::RZGate
260+
StandardGate::RZ
261261
};
262262

263263
let (packed, qubits) = if control_qubits.is_empty() {
@@ -281,10 +281,10 @@ fn multi_qubit_evolution(
281281
let params: SmallVec<[Param; 3]> =
282282
Python::with_gil(|py| smallvec![multiply_param(&time, -0.5, py)]);
283283
let (packed, qubits) = if control_qubits.len() == 1 {
284-
let gate: PackedOperation = StandardGate::PhaseGate.into();
284+
let gate: PackedOperation = StandardGate::Phase.into();
285285
(gate, vec![control_qubits[0]])
286286
} else {
287-
let controlled = add_control(StandardGate::PhaseGate, &params, &control_states[1..]);
287+
let controlled = add_control(StandardGate::Phase, &params, &control_states[1..]);
288288
control_qubits.reverse();
289289
(controlled, control_qubits)
290290
};
@@ -293,7 +293,7 @@ fn multi_qubit_evolution(
293293
if control_states[0] {
294294
// sandwich in X gates for the correct projector
295295
let x: Instruction = (
296-
StandardGate::XGate.into(),
296+
StandardGate::X.into(),
297297
smallvec![],
298298
vec![*qubits.last().unwrap()],
299299
empty_clbits.clone(),
@@ -430,7 +430,7 @@ fn cx_chain(qubits: Vec<Qubit>) -> Box<dyn DoubleEndedIterator<Item = Instructio
430430
.map(move |i| (qubits[i], qubits[i + 1]))
431431
.map(|(target, ctrl)| {
432432
(
433-
StandardGate::CXGate.into(),
433+
StandardGate::CX.into(),
434434
smallvec![],
435435
vec![ctrl, target],
436436
vec![],
@@ -462,7 +462,7 @@ fn cx_fountain(qubits: Vec<Qubit>) -> Box<dyn DoubleEndedIterator<Item = Instruc
462462
Box::new((1..num_terms).rev().map(move |i| {
463463
let ctrl = qubits[i];
464464
(
465-
StandardGate::CXGate.into(),
465+
StandardGate::CX.into(),
466466
smallvec![],
467467
vec![ctrl, first_qubit],
468468
vec![],

crates/accelerate/src/circuit_library/pauli_feature_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub fn pauli_feature_map(
134134
fn _get_h_layer(feature_dimension: u32) -> impl Iterator<Item = Instruction> {
135135
(0..feature_dimension).map(|i| {
136136
(
137-
StandardGate::HGate.into(),
137+
StandardGate::H.into(),
138138
smallvec![],
139139
vec![Qubit(i)],
140140
vec![] as Vec<Clbit>,

crates/accelerate/src/commutation_cancellation.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ use rustworkx_core::petgraph::stable_graph::NodeIndex;
2020
use smallvec::{smallvec, SmallVec};
2121

2222
use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType, Wire};
23-
use qiskit_circuit::operations::StandardGate::{
24-
CXGate, CYGate, CZGate, HGate, PhaseGate, RXGate, RZGate, SGate, TGate, U1Gate, XGate, YGate,
25-
ZGate,
26-
};
2723
use qiskit_circuit::operations::{Operation, Param, StandardGate};
2824
use qiskit_circuit::Qubit;
2925

@@ -37,10 +33,27 @@ static HALF_TURNS: [&str; 2] = ["z", "x"];
3733
static QUARTER_TURNS: [&str; 1] = ["s"];
3834
static EIGHTH_TURNS: [&str; 1] = ["t"];
3935

40-
static VAR_Z_MAP: [(&str, StandardGate); 3] = [("rz", RZGate), ("p", PhaseGate), ("u1", U1Gate)];
41-
static Z_ROTATIONS: [StandardGate; 6] = [PhaseGate, ZGate, U1Gate, RZGate, TGate, SGate];
42-
static X_ROTATIONS: [StandardGate; 2] = [XGate, RXGate];
43-
static SUPPORTED_GATES: [StandardGate; 5] = [CXGate, CYGate, CZGate, HGate, YGate];
36+
static VAR_Z_MAP: [(&str, StandardGate); 3] = [
37+
("rz", StandardGate::RZ),
38+
("p", StandardGate::Phase),
39+
("u1", StandardGate::U1),
40+
];
41+
static Z_ROTATIONS: [StandardGate; 6] = [
42+
StandardGate::Phase,
43+
StandardGate::Z,
44+
StandardGate::U1,
45+
StandardGate::RZ,
46+
StandardGate::T,
47+
StandardGate::S,
48+
];
49+
static X_ROTATIONS: [StandardGate; 2] = [StandardGate::X, StandardGate::RX];
50+
static SUPPORTED_GATES: [StandardGate; 5] = [
51+
StandardGate::CX,
52+
StandardGate::CY,
53+
StandardGate::CZ,
54+
StandardGate::H,
55+
StandardGate::Y,
56+
];
4457

4558
#[derive(Hash, Eq, PartialEq, Debug)]
4659
enum GateOrRotation {
@@ -242,7 +255,7 @@ pub(crate) fn cancel_commutations(
242255

243256
let new_op = match cancel_key.gate {
244257
GateOrRotation::ZRotation => z_var_gate.unwrap(),
245-
GateOrRotation::XRotation => &RXGate,
258+
GateOrRotation::XRotation => &StandardGate::RX,
246259
_ => unreachable!(),
247260
};
248261

crates/accelerate/src/commutation_checker.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,28 @@ use crate::QiskitError;
4242

4343
const fn build_supported_ops() -> [bool; STANDARD_GATE_SIZE] {
4444
let mut lut = [false; STANDARD_GATE_SIZE];
45-
lut[StandardGate::RXXGate as usize] = true;
46-
lut[StandardGate::RYYGate as usize] = true;
47-
lut[StandardGate::RZZGate as usize] = true;
48-
lut[StandardGate::RZXGate as usize] = true;
49-
lut[StandardGate::HGate as usize] = true;
50-
lut[StandardGate::XGate as usize] = true;
51-
lut[StandardGate::YGate as usize] = true;
52-
lut[StandardGate::ZGate as usize] = true;
53-
lut[StandardGate::SXGate as usize] = true;
54-
lut[StandardGate::SXdgGate as usize] = true;
55-
lut[StandardGate::TGate as usize] = true;
56-
lut[StandardGate::TdgGate as usize] = true;
57-
lut[StandardGate::SGate as usize] = true;
58-
lut[StandardGate::SdgGate as usize] = true;
59-
lut[StandardGate::CXGate as usize] = true;
60-
lut[StandardGate::CYGate as usize] = true;
61-
lut[StandardGate::CZGate as usize] = true;
62-
lut[StandardGate::SwapGate as usize] = true;
63-
lut[StandardGate::ISwapGate as usize] = true;
64-
lut[StandardGate::ECRGate as usize] = true;
65-
lut[StandardGate::CCXGate as usize] = true;
66-
lut[StandardGate::CSwapGate as usize] = true;
45+
lut[StandardGate::RXX as usize] = true;
46+
lut[StandardGate::RYY as usize] = true;
47+
lut[StandardGate::RZZ as usize] = true;
48+
lut[StandardGate::RZX as usize] = true;
49+
lut[StandardGate::H as usize] = true;
50+
lut[StandardGate::X as usize] = true;
51+
lut[StandardGate::Y as usize] = true;
52+
lut[StandardGate::Z as usize] = true;
53+
lut[StandardGate::SX as usize] = true;
54+
lut[StandardGate::SXdg as usize] = true;
55+
lut[StandardGate::T as usize] = true;
56+
lut[StandardGate::Tdg as usize] = true;
57+
lut[StandardGate::S as usize] = true;
58+
lut[StandardGate::Sdg as usize] = true;
59+
lut[StandardGate::CX as usize] = true;
60+
lut[StandardGate::CY as usize] = true;
61+
lut[StandardGate::CZ as usize] = true;
62+
lut[StandardGate::Swap as usize] = true;
63+
lut[StandardGate::ISwap as usize] = true;
64+
lut[StandardGate::ECR as usize] = true;
65+
lut[StandardGate::CCX as usize] = true;
66+
lut[StandardGate::CSwap as usize] = true;
6767
lut
6868
}
6969

@@ -76,20 +76,20 @@ static SUPPORTED_OP: [bool; STANDARD_GATE_SIZE] = build_supported_ops();
7676
// E.g. RX is generated by X and 2-pi periodic, while CRX is generated by CX and 4-pi periodic.
7777
const fn build_supported_rotations() -> [Option<Option<StandardGate>>; STANDARD_GATE_SIZE] {
7878
let mut lut = [None; STANDARD_GATE_SIZE];
79-
lut[StandardGate::RXGate as usize] = Some(Some(StandardGate::XGate));
80-
lut[StandardGate::RYGate as usize] = Some(Some(StandardGate::YGate));
81-
lut[StandardGate::RZGate as usize] = Some(Some(StandardGate::ZGate));
82-
lut[StandardGate::PhaseGate as usize] = Some(Some(StandardGate::ZGate));
83-
lut[StandardGate::U1Gate as usize] = Some(Some(StandardGate::ZGate));
84-
lut[StandardGate::CRXGate as usize] = Some(Some(StandardGate::CXGate));
85-
lut[StandardGate::CRYGate as usize] = Some(Some(StandardGate::CYGate));
86-
lut[StandardGate::CRZGate as usize] = Some(Some(StandardGate::CZGate));
87-
lut[StandardGate::CPhaseGate as usize] = Some(Some(StandardGate::CZGate));
79+
lut[StandardGate::RX as usize] = Some(Some(StandardGate::X));
80+
lut[StandardGate::RY as usize] = Some(Some(StandardGate::Y));
81+
lut[StandardGate::RZ as usize] = Some(Some(StandardGate::Z));
82+
lut[StandardGate::Phase as usize] = Some(Some(StandardGate::Z));
83+
lut[StandardGate::U1 as usize] = Some(Some(StandardGate::Z));
84+
lut[StandardGate::CRX as usize] = Some(Some(StandardGate::CX));
85+
lut[StandardGate::CRY as usize] = Some(Some(StandardGate::CY));
86+
lut[StandardGate::CRZ as usize] = Some(Some(StandardGate::CZ));
87+
lut[StandardGate::CPhase as usize] = Some(Some(StandardGate::CZ));
8888
// RXXGate, RYYGate, RZXGate, and RZZGate are supported by the commutation dictionary
89-
lut[StandardGate::RXXGate as usize] = Some(None);
90-
lut[StandardGate::RYYGate as usize] = Some(None);
91-
lut[StandardGate::RZXGate as usize] = Some(None);
92-
lut[StandardGate::RZZGate as usize] = Some(None);
89+
lut[StandardGate::RXX as usize] = Some(None);
90+
lut[StandardGate::RYY as usize] = Some(None);
91+
lut[StandardGate::RZX as usize] = Some(None);
92+
lut[StandardGate::RZZ as usize] = Some(None);
9393
lut
9494
}
9595

0 commit comments

Comments
 (0)