Skip to content
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

Port synth_cnot_phase_aam to Rust #12937

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8da594c
initial commit
MozammilQ Aug 10, 2024
193058b
added some code
MozammilQ Aug 10, 2024
2ae9379
refactor code
MozammilQ Aug 22, 2024
d7f41b7
Merge branch 'main' into port-synth_cnot_phase_aam-to-rust
MozammilQ Aug 22, 2024
60379b2
done some cargo-clippy linting
MozammilQ Aug 23, 2024
671b634
added code
MozammilQ Aug 26, 2024
ae91425
added some more code
MozammilQ Aug 27, 2024
64ce56b
refactor code
MozammilQ Aug 27, 2024
fe01589
refactor code
MozammilQ Aug 27, 2024
1ecab6d
refactor code
MozammilQ Aug 28, 2024
a217522
refactor code
MozammilQ Aug 28, 2024
4a36e1e
refactor code
MozammilQ Aug 29, 2024
0b681f9
refactor code
MozammilQ Aug 29, 2024
d2142f6
refactor code
MozammilQ Aug 29, 2024
8982c2c
refactor code
MozammilQ Aug 29, 2024
032b8c2
removed associated python code
MozammilQ Aug 29, 2024
583ca17
rust lint
MozammilQ Aug 29, 2024
d1394f4
added release note; added docstring to rust algo
MozammilQ Aug 30, 2024
6956e0f
rust lint
MozammilQ Aug 30, 2024
0091c98
refactor code
MozammilQ Aug 30, 2024
92bf2aa
Merge branch 'main' into port-synth_cnot_phase_aam-to-rust
MozammilQ Aug 30, 2024
6b2d31e
Merge branch 'main' into port-synth_cnot_phase_aam-to-rust
MozammilQ Aug 31, 2024
156da41
applied suggestion partially
MozammilQ Sep 1, 2024
b236d62
reverting suggestions
MozammilQ Sep 2, 2024
d28da2a
added a test
MozammilQ Sep 4, 2024
4c32291
Merge branch 'Qiskit:main' into port-synth_cnot_phase_aam-to-rust
MozammilQ Oct 28, 2024
1c7ad1e
Merge branch 'main' into port-synth_cnot_phase_aam-to-rust
MozammilQ Oct 29, 2024
68917a0
Merge branch 'Qiskit:main' into port-synth_cnot_phase_aam-to-rust
MozammilQ Nov 1, 2024
5973f85
replace vector with iterator
MozammilQ Nov 1, 2024
353e5ff
refactor code; lint
MozammilQ Nov 1, 2024
6262d16
refactor code
MozammilQ Nov 4, 2024
41a8906
refactor code
MozammilQ Nov 5, 2024
0cd01ce
refactor code
MozammilQ Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions crates/accelerate/src/synthesis/linear/pmh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ pub fn synth_cnot_count_full_pmh(
let mat: Array2<bool> = arrayview.to_owned();
let num_qubits = mat.nrows();

let instructions = _synth_cnot_count_full_pmh(mat, section_size);
let instructions = synth_pmh(mat, section_size);
CircuitData::from_standard_gates(py, num_qubits as u32, instructions, Param::Float(0.0))
}

type Instructions = (StandardGate, SmallVec<[Param; 3]>, SmallVec<[Qubit; 2]>);
pub fn _synth_cnot_count_full_pmh(mat: Array2<bool>, sec_size: Option<i64>) -> Vec<Instructions> {
pub fn synth_pmh(
mat: Array2<bool>,
sec_size: Option<i64>,
) -> impl DoubleEndedIterator<Item = Instructions> {
let mut mat = mat;
let num_qubits = mat.nrows(); // is a quadratic matrix

Expand All @@ -188,16 +191,15 @@ pub fn _synth_cnot_count_full_pmh(mat: Array2<bool>, sec_size: Option<i64>) -> V
let upper_cnots = lower_cnot_synth(mat.view_mut(), blocksize, true);

// iterator over the gates
let instructions = upper_cnots
.iter()
.map(|(i, j)| (*j, *i))
upper_cnots
.into_iter()
.map(|(i, j)| (j, i))
.chain(lower_cnots.into_iter().rev())
.map(|(ctrl, target)| {
(
StandardGate::CXGate,
smallvec![],
smallvec![Qubit(ctrl as u32), Qubit(target as u32)],
)
});
instructions.collect()
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

use crate::synthesis::linear::pmh::_synth_cnot_count_full_pmh;
use crate::synthesis::linear::pmh::synth_pmh;
use ndarray::Array2;
use numpy::PyReadonlyArray2;
use pyo3::{prelude::*, types::PyList};
Expand Down Expand Up @@ -100,8 +100,8 @@ pub fn synth_cnot_phase_aam(
}
}

let epsilion: usize = num_qubits;
let mut q = vec![(s, (0..num_qubits).collect::<Vec<usize>>(), epsilion)];
let epsilon: usize = num_qubits;
let mut q = vec![(s, (0..num_qubits).collect::<Vec<usize>>(), epsilon)];

while !q.is_empty() {
let (mut _s, mut _i, mut _ep) = q.pop().unwrap();
Expand Down Expand Up @@ -283,10 +283,7 @@ pub fn synth_cnot_phase_aam(
}

let state_bool = state.mapv(|x| x != 0);
let mut instrs = _synth_cnot_count_full_pmh(state_bool, section_size)
.into_iter()
.rev()
.collect();
let mut instrs = synth_pmh(state_bool, section_size).rev().collect();
Copy link
Contributor

@Cryoris Cryoris Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to write the instructions as iterator, instead of creating it as vector and pushing each element? That would allow us to avoid the collections and have CircuitData directly consume the iterator (and likely be a bit faster 🙂)

let insts = instructions.chain(synth_pmh(...).rev());
CircuitData::from_standard_gates(..., insts,...);

Copy link
Contributor Author

@MozammilQ MozammilQ Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have implemented Iterator for all the loops in the logic, the performance seems to be same, but the code has become very complex. and, the program crashes for inputs when num_qubits * depth > 14900. (This is stack-overflow, it worked when I increased the stack to 40MB by doing ulimit -s 40960 )
at the end I have no option other than to collect these iterator into a vector, because I have to get the residual state out of the iterator to feed into synth_pmh which has to be chained into final instructor iterator.
Still, using vec.push(...) doesn't seem bad, please have a look at it and guide me what to change next :)

instructions.append(&mut instrs);
CircuitData::from_standard_gates(py, num_qubits as u32, instructions, Param::Float(0.0))
}
Loading