-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Remove duplicated method on TwoQubitBasisDecomposer #15835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexanderivrii
merged 1 commit into
Qiskit:main
from
mtreinish:remove-duplicated-sequence-generation
Mar 23, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -713,100 +713,6 @@ impl TwoQubitBasisDecomposer { | |
| global_phase, | ||
| }) | ||
| } | ||
| /// Decompose a two-qubit ``unitary`` over fixed basis and :math:`SU(2)` using the best | ||
| /// approximation given that each basis application has a finite ``basis_fidelity``. | ||
| fn generate_sequence( | ||
| &self, | ||
| unitary: PyReadonlyArray2<Complex64>, | ||
| basis_fidelity: Option<f64>, | ||
| approximate: bool, | ||
| _num_basis_uses: Option<u8>, | ||
| ) -> PyResult<TwoQubitGateSequence> { | ||
| let basis_fidelity = if !approximate { | ||
| 1.0 | ||
| } else { | ||
| basis_fidelity.unwrap_or(self.basis_fidelity) | ||
| }; | ||
| let target_decomposed = | ||
| TwoQubitWeylDecomposition::new(unitary, Some(DEFAULT_FIDELITY), None)?; | ||
| let traces = self.traces(&target_decomposed); | ||
| let best_nbasis = traces | ||
| .into_iter() | ||
| .enumerate() | ||
| .map(|(idx, trace)| (idx, trace.trace_to_fid() * basis_fidelity.powi(idx as i32))) | ||
| .min_by(|(_idx1, fid1), (_idx2, fid2)| fid2.partial_cmp(fid1).unwrap()) | ||
| .unwrap() | ||
| .0; | ||
| let best_nbasis = _num_basis_uses.unwrap_or(best_nbasis as u8); | ||
| let decomposition = match best_nbasis { | ||
| 0 => decomp0_inner(&target_decomposed), | ||
| 1 => self.decomp1_inner(&target_decomposed), | ||
| 2 => self.decomp2_supercontrolled_inner(&target_decomposed), | ||
| 3 => self.decomp3_supercontrolled_inner(&target_decomposed), | ||
| _ => unreachable!("Invalid basis to use"), | ||
| }; | ||
| let pulse_optimize = self.pulse_optimize.unwrap_or(true); | ||
| let sequence = if pulse_optimize { | ||
| self.pulse_optimal_chooser(best_nbasis, &decomposition, &target_decomposed)? | ||
| } else { | ||
| None | ||
| }; | ||
| if let Some(seq) = sequence { | ||
| return Ok(seq); | ||
| } | ||
| let mut target_1q_basis_list = EulerBasisSet::new(); | ||
| target_1q_basis_list.add_basis(self.euler_basis); | ||
| let euler_decompositions: SmallVec<[Option<OneQubitGateSequence>; 8]> = decomposition | ||
| .iter() | ||
| .map(|decomp| { | ||
| unitary_to_gate_sequence_inner( | ||
| decomp.view(), | ||
| &target_1q_basis_list, | ||
| 0, | ||
| None, | ||
| true, | ||
| None, | ||
| ) | ||
| }) | ||
| .collect(); | ||
| let mut gates = Vec::with_capacity(TWO_QUBIT_SEQUENCE_DEFAULT_CAPACITY); | ||
| let mut global_phase = target_decomposed.global_phase; | ||
| global_phase -= best_nbasis as f64 * self.basis_decomposer.global_phase; | ||
| if best_nbasis == 2 { | ||
| global_phase += PI; | ||
| } | ||
| for i in 0..best_nbasis as usize { | ||
| if let Some(euler_decomp) = &euler_decompositions[2 * i] { | ||
| for gate in &euler_decomp.gates { | ||
| gates.push((gate.0.into(), gate.1.clone(), smallvec![0])); | ||
| } | ||
| global_phase += euler_decomp.global_phase | ||
| } | ||
| if let Some(euler_decomp) = &euler_decompositions[2 * i + 1] { | ||
| for gate in &euler_decomp.gates { | ||
| gates.push((gate.0.into(), gate.1.clone(), smallvec![1])); | ||
| } | ||
| global_phase += euler_decomp.global_phase | ||
| } | ||
| gates.push((self.gate.clone(), self.gate_params.clone(), smallvec![0, 1])); | ||
| } | ||
| if let Some(euler_decomp) = &euler_decompositions[2 * best_nbasis as usize] { | ||
| for gate in &euler_decomp.gates { | ||
| gates.push((gate.0.into(), gate.1.clone(), smallvec![0])); | ||
| } | ||
| global_phase += euler_decomp.global_phase | ||
| } | ||
| if let Some(euler_decomp) = &euler_decompositions[2 * best_nbasis as usize + 1] { | ||
| for gate in &euler_decomp.gates { | ||
| gates.push((gate.0.into(), gate.1.clone(), smallvec![1])); | ||
| } | ||
| global_phase += euler_decomp.global_phase | ||
| } | ||
| Ok(TwoQubitGateSequence { | ||
| gates, | ||
| global_phase, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| static K12R_ARR: GateArray1Q = [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps this should be moved to common? |
||
|
|
@@ -993,8 +899,8 @@ impl TwoQubitBasisDecomposer { | |
| approximate: bool, | ||
| _num_basis_uses: Option<u8>, | ||
| ) -> PyResult<DAGCircuit> { | ||
| let sequence = | ||
| self.generate_sequence(unitary, basis_fidelity, approximate, _num_basis_uses)?; | ||
| let array = unitary.as_array(); | ||
| let sequence = self.call_inner(array, basis_fidelity, approximate, _num_basis_uses)?; | ||
| let mut dag = DAGCircuit::with_capacity(2, 0, None, Some(sequence.gates.len()), None, None); | ||
| dag.set_global_phase_f64(sequence.global_phase); | ||
| dag.add_qubit_unchecked(ShareableQubit::new_anonymous())?; | ||
|
|
@@ -1036,8 +942,8 @@ impl TwoQubitBasisDecomposer { | |
| approximate: bool, | ||
| _num_basis_uses: Option<u8>, | ||
| ) -> PyResult<PyCircuitData> { | ||
| let sequence = | ||
| self.generate_sequence(unitary, basis_fidelity, approximate, _num_basis_uses)?; | ||
| let array = unitary.as_array(); | ||
| let sequence = self.call_inner(array, basis_fidelity, approximate, _num_basis_uses)?; | ||
| Ok(CircuitData::from_packed_operations( | ||
| 2, | ||
| 0, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, there is no real difference between the two functions, while the surviving function is more efficient handling this line - does not eagerly compute
best_nbasis.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, Shelly has already approved the changes as well. :)