Skip to content

Commit

Permalink
Use Qubit::new() and Clbit::new() where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
mtreinish committed Oct 7, 2024
1 parent 5c84176 commit 04f6233
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 95 deletions.
4 changes: 2 additions & 2 deletions crates/accelerate/src/commutation_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl CommutationChecker {
first_qargs
.iter()
.enumerate()
.map(|(i, q)| (q, Qubit(i as u32))),
.map(|(i, q)| (q, Qubit::new(i))),
);
let mut num_qubits = first_qargs.len() as u32;
for q in second_qargs {
Expand Down Expand Up @@ -574,7 +574,7 @@ fn get_relative_placement(
) -> SmallVec<[Option<Qubit>; 2]> {
let mut qubits_g2: HashMap<&Qubit, Qubit> = HashMap::with_capacity(second_qargs.len());
second_qargs.iter().enumerate().for_each(|(i_g1, q_g1)| {
qubits_g2.insert_unique_unchecked(q_g1, Qubit(i_g1 as u32));
qubits_g2.insert_unique_unchecked(q_g1, Qubit::new(i_g1));
});

first_qargs
Expand Down
6 changes: 2 additions & 4 deletions crates/accelerate/src/elide_permutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ fn run(py: Python, dag: &mut DAGCircuit) -> PyResult<Option<(DAGCircuit, Vec<usi
let cargs = dag.get_cargs(inst.clbits);
let mapped_qargs: Vec<Qubit> = qargs
.iter()
.map(|q| q.index())
.map(|q| mapping[q])
.map(|q| Qubit(q.try_into().unwrap()))
.map(|q| Qubit::new(mapping[q.index()]))
.collect();

new_dag.apply_operation_back(
Expand All @@ -92,7 +90,7 @@ fn run(py: Python, dag: &mut DAGCircuit) -> PyResult<Option<(DAGCircuit, Vec<usi
inst.params.as_deref().cloned(),
inst.extra_attrs.clone(),
#[cfg(feature = "cache_pygates")]
None,
inst.py_op.get().map(|x| x.clone_ref(py)),
)?;
}
}
Expand Down
12 changes: 4 additions & 8 deletions crates/accelerate/src/gates_in_basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn any_gate_missing_from_target(dag: &DAGCircuit, target: &Target) -> PyResult<b

if gate.op.control_flow() {
for block in gate.op.blocks() {
let block_qubits = (0..block.num_qubits()).map(|i| Qubit(i.try_into().unwrap()));
let block_qubits = (0..block.num_qubits()).map(Qubit::new);
let inner_wire_map = qargs
.iter()
.zip(block_qubits)
Expand Down Expand Up @@ -74,13 +74,9 @@ fn any_gate_missing_from_target(dag: &DAGCircuit, target: &Target) -> PyResult<b
}

// In the outer DAG, virtual and physical bits are the same thing.
let wire_map: HashMap<Qubit, PhysicalQubit> =
HashMap::from_iter((0..dag.num_qubits()).map(|i| {
(
Qubit(i.try_into().unwrap()),
PhysicalQubit::new(i.try_into().unwrap()),
)
}));
let wire_map: HashMap<Qubit, PhysicalQubit> = HashMap::from_iter(
(0..dag.num_qubits()).map(|i| (Qubit::new(i), PhysicalQubit::new(i.try_into().unwrap()))),
);

// Process the DAG.
for gate in dag.op_nodes(true) {
Expand Down
36 changes: 18 additions & 18 deletions crates/accelerate/src/synthesis/clifford/bm_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,52 +173,52 @@ fn reduce_cost(
gates.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(qubit0 as u32)],
smallvec![Qubit::new(qubit0)],
));
gates.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(qubit0 as u32)],
smallvec![Qubit::new(qubit0)],
));
} else if n0 == 2 {
gates.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(qubit0 as u32)],
smallvec![Qubit::new(qubit0)],
));
gates.push((
StandardGate::SdgGate,
smallvec![],
smallvec![Qubit(qubit0 as u32)],
smallvec![Qubit::new(qubit0)],
));
}
if n1 == 1 {
gates.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(qubit1 as u32)],
smallvec![Qubit::new(qubit1)],
));
gates.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(qubit1 as u32)],
smallvec![Qubit::new(qubit1)],
));
} else if n1 == 2 {
gates.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(qubit1 as u32)],
smallvec![Qubit::new(qubit1)],
));
gates.push((
StandardGate::SdgGate,
smallvec![],
smallvec![Qubit(qubit1 as u32)],
smallvec![Qubit::new(qubit1)],
));
}
gates.push((
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(qubit0 as u32), Qubit(qubit1 as u32)],
smallvec![Qubit::new(qubit0), Qubit::new(qubit1)],
));

return Ok((reduced_cliff, new_cost));
Expand All @@ -242,19 +242,19 @@ fn decompose_clifford_1q(cliff: &Clifford, gates: &mut CliffordGatesVec, output_
gates.push((
StandardGate::ZGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
} else if !destab_phase && stab_phase {
gates.push((
StandardGate::XGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
} else if destab_phase && stab_phase {
gates.push((
StandardGate::YGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
}

Expand All @@ -268,39 +268,39 @@ fn decompose_clifford_1q(cliff: &Clifford, gates: &mut CliffordGatesVec, output_
gates.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
}
} else if !stab_z && stab_x {
if destab_x {
gates.push((
StandardGate::SdgGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
}
gates.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
} else {
if !destab_z {
gates.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
}
gates.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
gates.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(output_qubit as u32)],
smallvec![Qubit::new(output_qubit)],
));
}
}
Expand Down
43 changes: 20 additions & 23 deletions crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,28 +214,28 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
self.symplectic_matrix.prepend_s(*qubit);
}
SingleQubitGate::GateH => {
gate_seq.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
self.symplectic_matrix.prepend_h(*qubit);
}
SingleQubitGate::GateSH => {
gate_seq.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
gate_seq.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
self.symplectic_matrix.prepend_s(*qubit);
self.symplectic_matrix.prepend_h(*qubit);
Expand All @@ -244,12 +244,12 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
gate_seq.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
self.symplectic_matrix.prepend_h(*qubit);
self.symplectic_matrix.prepend_s(*qubit);
Expand All @@ -258,17 +258,17 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
gate_seq.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
gate_seq.push((
StandardGate::SGate,
smallvec![],
smallvec![Qubit(*qubit as u32)],
smallvec![Qubit::new(*qubit)],
));
self.symplectic_matrix.prepend_s(*qubit);
self.symplectic_matrix.prepend_h(*qubit);
Expand Down Expand Up @@ -304,7 +304,7 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::SwapGate,
smallvec![],
smallvec![Qubit(min_qubit as u32), Qubit(qubit_a as u32)],
smallvec![Qubit::new(min_qubit), Qubit::new(qubit_a)],
));
self.symplectic_matrix.prepend_swap(min_qubit, qubit_a);

Expand All @@ -327,7 +327,7 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(min_qubit as u32), Qubit(qubit as u32)],
smallvec![Qubit::new(min_qubit), Qubit::new(qubit)],
));
self.symplectic_matrix.prepend_cx(min_qubit, qubit);
}
Expand All @@ -336,7 +336,7 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(qubit as u32), Qubit(min_qubit as u32)],
smallvec![Qubit::new(qubit), Qubit::new(min_qubit)],
));
self.symplectic_matrix.prepend_cx(qubit, min_qubit);
}
Expand All @@ -347,7 +347,7 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(qubit_b as u32), Qubit(*qubit as u32)],
smallvec![Qubit::new(qubit_b), Qubit::new(*qubit)],
));
self.symplectic_matrix.prepend_cx(qubit_b, *qubit);
}
Expand All @@ -358,21 +358,21 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(min_qubit as u32), Qubit(qubit_b as u32)],
smallvec![Qubit::new(min_qubit), Qubit::new(qubit_b)],
));
self.symplectic_matrix.prepend_cx(min_qubit, qubit_b);

gate_seq.push((
StandardGate::HGate,
smallvec![],
smallvec![Qubit(qubit_b as u32)],
smallvec![Qubit::new(qubit_b)],
));
self.symplectic_matrix.prepend_h(qubit_b);

gate_seq.push((
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(qubit_b as u32), Qubit(min_qubit as u32)],
smallvec![Qubit::new(qubit_b), Qubit::new(min_qubit)],
));
self.symplectic_matrix.prepend_cx(qubit_b, min_qubit);
}
Expand All @@ -387,8 +387,8 @@ impl GreedyCliffordSynthesis<'_> {
StandardGate::CXGate,
smallvec![],
smallvec![
Qubit(a_qubits[2 * qubit + 1] as u32),
Qubit(a_qubits[2 * qubit] as u32)
Qubit::new(a_qubits[2 * qubit + 1]),
Qubit::new(a_qubits[2 * qubit])
],
));
self.symplectic_matrix
Expand All @@ -397,18 +397,15 @@ impl GreedyCliffordSynthesis<'_> {
gate_seq.push((
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(a_qubits[2 * qubit] as u32), Qubit(min_qubit as u32)],
smallvec![Qubit::new(a_qubits[2 * qubit]), Qubit::new(min_qubit)],
));
self.symplectic_matrix
.prepend_cx(a_qubits[2 * qubit], min_qubit);

gate_seq.push((
StandardGate::CXGate,
smallvec![],
smallvec![
Qubit(min_qubit as u32),
Qubit(a_qubits[2 * qubit + 1] as u32)
],
smallvec![Qubit::new(min_qubit), Qubit::new(a_qubits[2 * qubit + 1])],
));
self.symplectic_matrix
.prepend_cx(min_qubit, a_qubits[2 * qubit + 1]);
Expand Down
6 changes: 3 additions & 3 deletions crates/accelerate/src/synthesis/clifford/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,21 @@ pub fn adjust_final_pauli_gates(
gate_seq.push((
StandardGate::YGate,
smallvec![],
smallvec![Qubit(qubit as u32)],
smallvec![Qubit::new(qubit)],
));
} else if delta_phase_pre[qubit] {
// println!("=> Adding Z-gate on {}", qubit);
gate_seq.push((
StandardGate::ZGate,
smallvec![],
smallvec![Qubit(qubit as u32)],
smallvec![Qubit::new(qubit)],
));
} else if delta_phase_pre[qubit + num_qubits] {
// println!("=> Adding X-gate on {}", qubit);
gate_seq.push((
StandardGate::XGate,
smallvec![],
smallvec![Qubit(qubit as u32)],
smallvec![Qubit::new(qubit)],
));
}
}
Expand Down
Loading

0 comments on commit 04f6233

Please sign in to comment.