-
Notifications
You must be signed in to change notification settings - Fork 1
Create python mappings for clifford tableau #32
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from qiskit.quantum_info import Clifford | ||
| from qiskit import QuantumCircuit | ||
|
|
||
| from synpy.qiskit.plugin import SynPyCliffordPlugin | ||
|
|
||
|
|
||
| def test_qiskit_bell() -> None: | ||
| qc = QuantumCircuit(2) | ||
| qc.h(0) | ||
| qc.cx(0, 1) | ||
| cliff = Clifford(qc) | ||
|
|
||
| plugin = SynPyCliffordPlugin() | ||
| circ = plugin.run(cliff, None, None, []) | ||
| # circ.draw() | ||
|
|
||
| assert circ == qc |
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from qiskit.transpiler import CouplingMap, Target | ||
| from qiskit.transpiler.passes.synthesis.plugin import HighLevelSynthesisPlugin | ||
| from qiskit.quantum_info import Clifford | ||
| from qiskit import QuantumCircuit | ||
|
|
||
| from synpy.synpy_rust import PyCliffordTableau | ||
| from synpy.utils import pycommand_to_qasm | ||
|
|
||
|
|
||
| class SynPyCliffordPlugin(HighLevelSynthesisPlugin): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
|
|
||
| def run(self, clifford: Clifford, coupling_map: CouplingMap, target: Target, qubits: list) -> QuantumCircuit: | ||
| n = clifford.num_qubits | ||
| tableau_x = clifford.tableau[:, :n] | ||
| tableau_z = clifford.tableau[:, n : 2 * n] | ||
|
|
||
| pauli_columns = ["".join(["IZXY"[2 * x + z] for x, z in zip(col_x, col_z)]) for col_x, col_z in zip(tableau_x.T, tableau_z.T)] | ||
| signs = clifford.tableau[:, -1].tolist() | ||
|
|
||
| # Convert Clifford to CliffordTableau | ||
| synpy_tableau = PyCliffordTableau.from_parts(pauli_columns, signs) | ||
|
|
||
| # Synthesize CliffordTableau | ||
| commands = synpy_tableau.synthesize() | ||
| qasm = pycommand_to_qasm(n, commands) | ||
| return QuantumCircuit.from_qasm_str(qasm) |
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
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
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 |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| mod synthesis; | ||
| mod tableau; | ||
| mod validation; | ||
|
|
||
| use crate::synthesis::synthesize_pauli_exponential; | ||
| use crate::synthesis::{PyCommand, PyPauliString}; | ||
| use crate::tableau::PyCliffordTableau; | ||
| use pyo3::prelude::{PyModule, PyModuleMethods}; | ||
| use pyo3::{pymodule, wrap_pyfunction, Bound, FromPyObject, PyRef, PyResult, Python}; | ||
| use std::ops::Deref; | ||
| use syn::ir::Synthesizer; | ||
| use pyo3::{pymodule, wrap_pyfunction, Bound, PyResult}; | ||
|
|
||
| #[pymodule] | ||
| #[pyo3(name = "synpy_rust")] | ||
| fn synpy_rust(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
| m.add_class::<PyPauliString>()?; | ||
| m.add_class::<PyCommand>()?; | ||
| let _ = m.add_function(wrap_pyfunction!(synthesize_pauli_exponential, m)?); | ||
| m.add_class::<PyCliffordTableau>()?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
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
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| use crate::synthesis::{CommandCollector, PyCommand, Synthesize}; | ||
|
|
||
| use bitvec::prelude::BitVec; | ||
| use pyo3::basic::CompareOp; | ||
| use pyo3::prelude::*; | ||
| use pyo3::PyResult; | ||
| use syn::data_structures::CliffordTableau as CliffordTableau; | ||
| use syn::data_structures::PauliString; | ||
| use syn::data_structures::PropagateClifford; | ||
|
|
||
| use syn::ir::clifford_tableau::NaiveCliffordSynthesizer; | ||
| use syn::ir::Synthesizer; | ||
|
|
||
| #[pyclass(unsendable)] | ||
| pub struct PyCliffordTableau { | ||
| tableau: CliffordTableau, | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl PyCliffordTableau { | ||
| #[new] | ||
| fn new(n: usize) -> Self { | ||
| PyCliffordTableau { | ||
| tableau: CliffordTableau::new(n), | ||
| } | ||
| } | ||
|
|
||
| #[staticmethod] | ||
| pub fn from_parts(pauli_strings: Vec<String>, signs: Vec<bool>) -> Self { | ||
| let pauli_columns: Vec<PauliString> = pauli_strings | ||
| .iter() | ||
| .map(|pauli_string| PauliString::from_text(pauli_string)) | ||
| .collect(); | ||
| let signs_bitvec: BitVec = signs.iter().copied().collect(); | ||
| let tableau = CliffordTableau::from_parts(pauli_columns, signs_bitvec); | ||
|
|
||
| PyCliffordTableau { tableau } | ||
| } | ||
|
|
||
| pub fn size(&self) -> usize { | ||
| self.tableau.size() | ||
| } | ||
|
|
||
| pub(crate) fn compose(&self, rhs: &Self) -> Self { | ||
| PyCliffordTableau { | ||
| tableau: self.tableau.compose(&rhs.tableau), | ||
| } | ||
| } | ||
|
|
||
| fn __richcmp__(&self, other: &PyCliffordTableau, op: CompareOp) -> PyResult<bool> { | ||
| match op { | ||
| CompareOp::Eq => Ok(self.tableau == other.tableau), | ||
| CompareOp::Ne => Ok(self.tableau != other.tableau), | ||
| _ => Ok(false), | ||
| } | ||
| } | ||
|
|
||
| pub fn synthesize(&self) -> Vec<PyCommand> { | ||
| <Self as Synthesize>::synthesize(self) | ||
| } | ||
| } | ||
|
|
||
| impl PropagateClifford for PyCliffordTableau { | ||
| fn cx(&mut self, control: usize, target: usize) -> &mut Self { | ||
| self.tableau.cx(control, target); | ||
| self | ||
| } | ||
|
|
||
| fn s(&mut self, target: usize) -> &mut Self { | ||
| self.tableau.s(target); | ||
| self | ||
| } | ||
|
|
||
| fn v(&mut self, target: usize) -> &mut Self { | ||
| self.tableau.v(target); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Synthesize for PyCliffordTableau { | ||
| fn synthesize(&self) -> Vec<PyCommand> { | ||
| let mut tracker = CommandCollector::new(); | ||
| let mut synthesizer = NaiveCliffordSynthesizer::default(); | ||
| synthesizer.synthesize(self.tableau.clone(), &mut tracker); | ||
| tracker.commands() | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.