-
Notifications
You must be signed in to change notification settings - Fork 0
41 visualization #46
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
base: main
Are you sure you want to change the base?
41 visualization #46
Conversation
Ectras
left a comment
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.
I like the visualization, maybe someone else can give their opinion as well. My comments are mostly about code quality. The comments are intentionally nitpicky, to help learn idiomatic Rust :)
|
|
||
| for i in 0..self.size() { | ||
| print!("QB{} ||", i + 1); | ||
| let column = self.pauli_column(i).to_string(); |
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.
In my opinion, it's a bit risky to rely on the string representation of PauliString. I'd rather construct the string here from the ground up, using e.g. pauli_letter to get the letters in the Pauli string.
|
Actually, I find the headers of the Clifford tableau output a bit confusing. I.e., I'm talking about the It looks like the columns of a normal Clifford tableau. But we print the columns as rows, i.e., the whole thing is transposed. I don't think the headers make sense then. The first |
…y removing vec allocation
|
A general comment on this task is I am not so sure on how do I use PyO3 to port these display functionality to python ? Since I have already implement the |
examples/plot_data_structure.rs
Outdated
| // Stab: ZZZ, -YIY, XIX | ||
| // Destab: -IXI, XXI, IYY |
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.
I don't think these stabilizers and destabilizers correspond ot the tableau you are creating in the code
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.
I fixed it in the latest commit
examples/plot_data_structure.rs
Outdated
| let ham = vec![("IZZZ", 0.3)]; | ||
| let pp = PauliPolynomial::from_hamiltonian(ham); | ||
| let ct = CliffordTableau::new(4); | ||
| let pe = PauliExponential::new(VecDeque::from([pp]), ct); |
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.
You can reuse the pp and tableau from before
examples/plot_data_structure.rs
Outdated
| // //visualize_pauli_exponential complex | ||
| let ham = vec![("IXYZ", 0.3), ("XXII", 0.7), ("YYII", 0.12)]; | ||
|
|
||
| let pauli_polynomial = PauliPolynomial::from_hamiltonian(ham); | ||
| let clifford_tableau = CliffordTableau::new(4); | ||
| let complex_pe = PauliExponential::new(VecDeque::from([pauli_polynomial]), clifford_tableau); | ||
| println!("{}", complex_pe); |
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.
then this would be redundant
| for (i, column) in self.pauli_columns.iter().enumerate() { | ||
| write!(f, "QB{} ||", i)?; | ||
| let mut out = String::new(); | ||
| let mut letter_count = 0; | ||
| for j in 0..column.len() { | ||
| // let letter = column.pauli(j); | ||
| match column.pauli(j) { |
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.
It looks like you are printing the tableau transposed. i.e. the columns are printed as rows.
| ct.to_string(), | ||
| "CliffordTableau(3)\nZ Y I I Z Z\nZ I X X I I\nZ Y Y I I Z\n+ - + - + +" | ||
| " || X1 Z1| X2 Z2| X3 Z3|\n+/- || + - | + - | + + |\nQB0 || Z Y | I I | Z Z | \nQB1 || Z I | X X | I I | \nQB2 || Z Y | Y I | I Z | \n\n" |
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.
Yeah, you are printing the transpose, which was the limitation of the debug formatter because it's easy to print the columns are rows.
Something like this:
" || Stabilizers | Destabilizers |\n QB0 || + Z Z Z | - I X I | \nQB1 || - Y I Y | + Z I I | \nQB2 || + I X Y | + Z I Z | \n\n"
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.
should be correct now
| let pp = setup_sample_pp(); | ||
| assert_eq!( | ||
| pp.to_string(), | ||
| "Angles | 0.300 | 0.700 | 0.120 |\nQubit 0| I | X | Y |\nQubit 1| Z | Y | X |\nQubit 2| Y | I | X |\n\n" |
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.
Be more consistent with the formatting wrt the tableau. "QB0" vs "Qubit 0".
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.
fixed
| @@ -29,6 +30,17 @@ impl PauliExponential { | |||
| } | |||
| } | |||
|
|
|||
| impl fmt::Display for PauliExponential { | |||
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.
missing test.
It would be cool if you can make it look something like this:
|| 0.2 | 0.4 || 0.12 || Stabilizers | Destabilizers ||
QB0 || X | I || Z || + Z Z Z | - I X I ||
QB1 || I | I || Z || - Y I Y | + Z I I ||
QB2 || I | Z || Z || + I X Y | + Z I Z ||
Where the || separate the pauli polynomials in the pauli exponential, followed by the stabilizers and destabilizers
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.
This would require a refactor of the other formatters so that they can give partial solutions per line that you can glue together.
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.
fixed
merge latest development of code for easier conflict management
…r complain compose is a private method
Looks like you'll find how to do that here: https://stackoverflow.com/questions/62666926/str-function-of-class-ported-from-rust-to-python-using-pyo3-doesnt-get-used @Ectras Is the stackoverflow a good source? |
… easier integration with pe display
I don't think it's necessary (anymore) to implement a specific trait, just name the functions the same as in Python. See also https://pyo3.rs/v0.15.1/class/protocols. Here is a small example: use std::fmt;
use pyo3::prelude::*;
struct RustType {
num: i32,
}
impl fmt::Display for RustType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Rust says {}", self.num)
}
}
#[pyclass]
struct Wrapper(RustType);
#[pymethods]
impl Wrapper {
#[new]
fn new(num: i32) -> Self {
Wrapper(RustType { num })
}
// This method is recognized as magic function by pyo3
fn __str__(&self) -> PyResult<String> {
// to_string() uses the Display trait
Ok(self.0.to_string())
}
} |
…ean up comment and add some more tests
|
I am pretty happy with the Rust print function at the moment, but I still struggle with PyO3. from @Ectras comment it seems that I only need to define str for each object type ? But I have not seen the PauliPolynomial and PauliExponential implementation in the synpy folder. what would probably help me to move forward is may be a demonstration of how to write a Wrapper for the PP and PE, bind it, and a small python example of object instantiation and usage. I checked out @Aerylia pull request on the user guide to find some inspiration but it seems that she use pauliopt library instead ? |
|
I think the output looks nice now. Does printing "empty things" (where this makes sense) also work? Also, you could make the alignment independent of the number of qubits by using I wouldn't worry about the case where the max line length of the output media (e.g. the terminal) is not sufficient. I don't know any tool that cares about this. If the users want large output without any text wrapping, they should pipe the output to a file. |
|
Ah, I believe the part that has the X matrix is the Destabilizers and Z matrix is stabilizers because Z stabilizes the computational basis. X|0> = |1> and Z|0> = |0> |
I checked the code, apparently the small example print correctly according to the comment left by previous developer. For the big example, X and Z was called using 'CliffordTableau::new'. I probably need a bit more discussion on this |
|
On the task of printing empty things:
|


I added some basic visualization method to the 3 data structures assigned. Since I print them in the terminal, the editing is minimal and I made some deliberate choice to hard code decimal points to keep the formatting consistent. I would need some comment on how to improve the code for more readability and flexibility.