Skip to content

Commit c2d127c

Browse files
aborgna-qABorgna
andauthored
fix: Lints and warnings (zxcalc#16)
This PR fixes all the warnings produced by `cargo check` and `cargo clippy --all-targets --all-features`. --------- Co-authored-by: Agustin Borgna <[email protected]>
1 parent 032e8bd commit c2d127c

26 files changed

+621
-471
lines changed

pybindings/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use num::Rational;
1+
use num::Rational64;
22
use pyo3::prelude::*;
33
use pyo3::wrap_pyfunction;
44
use quizx::extract::ToCircuit;
@@ -40,7 +40,7 @@ fn full_simp(g: &mut VecGraph) {
4040
#[pyfunction]
4141
fn extract_circuit(g: &mut VecGraph) -> Circuit {
4242
Circuit {
43-
c: g.g.into_circuit().unwrap(),
43+
c: g.g.to_circuit().unwrap(),
4444
s: None,
4545
}
4646
}
@@ -158,7 +158,7 @@ impl VecGraph {
158158
3 => VType::H,
159159
_ => VType::B,
160160
};
161-
let phase = Rational::new(phase.0, phase.1);
161+
let phase = Rational64::new(phase.0, phase.1);
162162
self.g.add_vertex_with_data(VData {
163163
ty,
164164
phase,
@@ -238,11 +238,11 @@ impl VecGraph {
238238
}
239239

240240
fn set_phase(&mut self, v: usize, phase: (isize, isize)) {
241-
self.g.set_phase(v, Rational::new(phase.0, phase.1));
241+
self.g.set_phase(v, Rational64::new(phase.0, phase.1));
242242
}
243243

244244
fn add_to_phase(&mut self, v: usize, phase: (isize, isize)) {
245-
self.g.add_to_phase(v, Rational::new(phase.0, phase.1));
245+
self.g.add_to_phase(v, Rational64::new(phase.0, phase.1));
246246
}
247247

248248
fn qubit(&mut self, v: usize) -> i32 {

quizx/src/basic_rules.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use crate::graph::*;
3030
use crate::scalar::*;
3131
use num::traits::Zero;
32-
use num::Rational;
32+
use num::Rational64;
3333
use rustc_hash::FxHashSet;
3434
use std::iter::FromIterator;
3535

@@ -293,7 +293,7 @@ pub fn local_comp_unchecked(g: &mut impl GraphLike, v: V) {
293293

294294
let x = ns.len() as i32;
295295
g.scalar_mut().mul_sqrt2_pow(((x - 1) * (x - 2)) / 2);
296-
g.scalar_mut().mul_phase(Rational::new(*p.numer(), 4));
296+
g.scalar_mut().mul_phase(Rational64::new(*p.numer(), 4));
297297
}
298298

299299
checked_rule1!(check_local_comp, local_comp_unchecked, local_comp);
@@ -350,7 +350,7 @@ pub fn pivot_unchecked(g: &mut impl GraphLike, v0: V, v1: V) {
350350
g.scalar_mut().mul_sqrt2_pow((x - 2) * (y - 2));
351351

352352
if !p0.is_zero() && !p1.is_zero() {
353-
g.scalar_mut().mul_phase(Rational::new(1, 1));
353+
g.scalar_mut().mul_phase(Rational64::new(1, 1));
354354
}
355355
}
356356

@@ -366,7 +366,7 @@ fn unfuse_boundary(g: &mut impl GraphLike, v: V, b: V) {
366366
}
367367
let vd = VData {
368368
ty: VType::Z,
369-
phase: Rational::zero(),
369+
phase: Rational64::zero(),
370370
row: g.row(v),
371371
qubit: g.qubit(v),
372372
};
@@ -385,14 +385,14 @@ fn unfuse_gadget(g: &mut impl GraphLike, v: V) {
385385
}
386386
let vd = VData {
387387
ty: VType::Z,
388-
phase: Rational::zero(),
388+
phase: Rational64::zero(),
389389
row: g.row(v),
390390
qubit: g.qubit(v),
391391
};
392392
let v1 = g.add_vertex_with_data(vd);
393393
let v2 = g.add_vertex_with_data(vd);
394394
g.set_phase(v2, g.phase(v));
395-
g.set_phase(v, Rational::zero());
395+
g.set_phase(v, Rational64::zero());
396396
g.add_edge_with_type(v, v1, EType::H);
397397
g.add_edge_with_type(v1, v2, EType::H);
398398
}
@@ -578,7 +578,7 @@ pub fn remove_pair_unchecked(g: &mut impl GraphLike, v0: V, v1: V) {
578578
*g.scalar_mut() *= ScalarN::one_plus_phase(p0 + p1);
579579
// different colors
580580
} else {
581-
let p2 = Rational::one() + &p0 + &p1;
581+
let p2 = Rational64::one() + p0 + p1;
582582
*g.scalar_mut() *= ScalarN::one()
583583
+ ScalarN::from_phase(p0)
584584
+ ScalarN::from_phase(p1)
@@ -599,7 +599,7 @@ mod tests {
599599
use super::*;
600600
use crate::tensor::*;
601601
use crate::vec_graph::Graph;
602-
use num::Rational;
602+
use num::Rational64;
603603

604604
#[test]
605605
fn spider_fusion_simple() {
@@ -614,8 +614,8 @@ mod tests {
614614
g.add_vertex(VType::B),
615615
];
616616

617-
g.set_phase(vs[2], Rational::new(1, 2));
618-
g.set_phase(vs[3], Rational::new(1, 4));
617+
g.set_phase(vs[2], Rational64::new(1, 2));
618+
g.set_phase(vs[3], Rational64::new(1, 4));
619619

620620
g.add_edge(vs[0], vs[2]);
621621
g.add_edge(vs[1], vs[2]);
@@ -639,7 +639,7 @@ mod tests {
639639

640640
assert_eq!(g.to_tensor4(), h.to_tensor4());
641641

642-
assert_eq!(g.phase(vs[2]), Rational::new(3, 4));
642+
assert_eq!(g.phase(vs[2]), Rational64::new(3, 4));
643643
}
644644

645645
#[test]
@@ -657,8 +657,8 @@ mod tests {
657657
g.add_vertex(VType::B),
658658
];
659659

660-
g.set_phase(vs[2], Rational::new(1, 2));
661-
g.set_phase(vs[3], Rational::new(1, 4));
660+
g.set_phase(vs[2], Rational64::new(1, 2));
661+
g.set_phase(vs[3], Rational64::new(1, 4));
662662

663663
g.add_edge(vs[0], vs[2]);
664664
g.add_edge(vs[1], vs[2]);
@@ -693,14 +693,14 @@ mod tests {
693693
println!("\n\nth =\n{}", th);
694694
assert_eq!(tg, th);
695695

696-
assert_eq!(g.phase(vs[2]), Rational::new(3, 4));
696+
assert_eq!(g.phase(vs[2]), Rational64::new(3, 4));
697697
}
698698

699699
#[test]
700700
fn local_comp_1() {
701701
let mut g = Graph::new();
702702
g.add_vertex(VType::Z);
703-
g.set_phase(0, Rational::new(1, 2));
703+
g.set_phase(0, Rational64::new(1, 2));
704704
g.add_vertex(VType::Z);
705705
g.add_vertex(VType::Z);
706706
g.add_vertex(VType::Z);
@@ -738,12 +738,12 @@ mod tests {
738738
assert_eq!(tg, th);
739739

740740
for i in 1..5 {
741-
assert_eq!(g.phase(i), Rational::new(-1, 2));
741+
assert_eq!(g.phase(i), Rational64::new(-1, 2));
742742
}
743743

744744
assert_eq!(
745745
*g.scalar(),
746-
Scalar::sqrt2_pow((4 - 1) * (4 - 2) / 2) * Scalar::from_phase(Rational::new(1, 4))
746+
Scalar::sqrt2_pow((4 - 1) * (4 - 2) / 2) * Scalar::from_phase(Rational64::new(1, 4))
747747
);
748748

749749
let h = g.clone();
@@ -759,7 +759,7 @@ mod tests {
759759
for _ in 0..7 {
760760
g.add_vertex(VType::Z);
761761
}
762-
g.set_phase(3, Rational::new(1, 1));
762+
g.set_phase(3, Rational64::new(1, 1));
763763
for i in 0..3 {
764764
g.add_edge_with_type(i, 3, EType::H);
765765
}
@@ -783,8 +783,8 @@ mod tests {
783783
assert_eq!(h.num_vertices(), 5);
784784
assert_eq!(h.num_edges(), 6);
785785

786-
assert_eq!(h.phase(0), Rational::new(0, 1));
787-
assert_eq!(h.phase(6), Rational::new(1, 1));
786+
assert_eq!(h.phase(0), Rational64::new(0, 1));
787+
assert_eq!(h.phase(6), Rational64::new(1, 1));
788788

789789
let mut inputs: Vec<usize> = Vec::new();
790790
let mut outputs: Vec<usize> = Vec::new();
@@ -816,8 +816,8 @@ mod tests {
816816
for _ in 0..7 {
817817
g.add_vertex(VType::Z);
818818
}
819-
g.set_phase(3, Rational::new(1, 1));
820-
g.set_phase(4, Rational::new(1, 1));
819+
g.set_phase(3, Rational64::new(1, 1));
820+
g.set_phase(4, Rational64::new(1, 1));
821821
for i in 0..3 {
822822
g.add_edge_with_type(i, 3, EType::H);
823823
}
@@ -835,8 +835,8 @@ mod tests {
835835
assert_eq!(g.num_vertices(), 5);
836836
assert_eq!(g.num_edges(), 6);
837837

838-
assert_eq!(g.phase(0), Rational::new(1, 1));
839-
assert_eq!(g.phase(6), Rational::new(1, 1));
838+
assert_eq!(g.phase(0), Rational64::new(1, 1));
839+
assert_eq!(g.phase(6), Rational64::new(1, 1));
840840
}
841841

842842
#[test]
@@ -847,8 +847,8 @@ mod tests {
847847
g.add_vertex(VType::Z);
848848
}
849849
g.set_vertex_type(0, VType::B);
850-
// g.set_phase(3, Rational::new(1,1));
851-
// g.set_phase(4, Rational::new(1,1));
850+
// g.set_phase(3, Rational64::new(1,1));
851+
// g.set_phase(4, Rational64::new(1,1));
852852
for i in 0..3 {
853853
g.add_edge_with_type(i, 3, EType::H);
854854
}
@@ -881,8 +881,8 @@ mod tests {
881881
for _ in 0..7 {
882882
g.add_vertex(VType::Z);
883883
}
884-
g.set_phase(3, Rational::new(1, 1));
885-
g.set_phase(4, Rational::new(1, 4));
884+
g.set_phase(3, Rational64::new(1, 1));
885+
g.set_phase(4, Rational64::new(1, 4));
886886
for i in 0..3 {
887887
g.add_edge_with_type(i, 3, EType::H);
888888
}
@@ -911,51 +911,51 @@ mod tests {
911911
fn gadget_fusion_1() {
912912
// fuse gadgets of various sizes
913913
for n in 1..5 {
914-
let mut g = Graph::new();
915-
let bs: Vec<_> = (0..n).map(|_| g.add_vertex(VType::B)).collect();
916-
let vs: Vec<_> = (0..n).map(|_| g.add_vertex(VType::Z)).collect();
917-
let gs: Vec<_> = (0..2).map(|_| g.add_vertex(VType::Z)).collect();
918-
let ps: Vec<_> = (0..2).map(|_| g.add_vertex(VType::Z)).collect();
919-
g.set_inputs(bs.clone());
914+
let mut graph = Graph::new();
915+
let bs: Vec<_> = (0..n).map(|_| graph.add_vertex(VType::B)).collect();
916+
let vs: Vec<_> = (0..n).map(|_| graph.add_vertex(VType::Z)).collect();
917+
let gs: Vec<_> = (0..2).map(|_| graph.add_vertex(VType::Z)).collect();
918+
let ps: Vec<_> = (0..2).map(|_| graph.add_vertex(VType::Z)).collect();
919+
graph.set_inputs(bs.clone());
920920

921921
for i in 0..n {
922-
g.add_edge(bs[i], vs[i]);
922+
graph.add_edge(bs[i], vs[i]);
923923
}
924-
for j in 0..2 {
925-
g.add_edge_with_type(gs[j], ps[j], EType::H);
926-
for i in 0..n {
927-
g.add_edge_with_type(vs[i], gs[j], EType::H);
924+
for (&g, &p) in gs.iter().zip(ps.iter()) {
925+
graph.add_edge_with_type(g, p, EType::H);
926+
for &v in &vs {
927+
graph.add_edge_with_type(v, g, EType::H);
928928
}
929929
}
930930

931-
g.set_phase(ps[0], Rational::new(1, 4));
932-
g.set_phase(ps[1], Rational::new(1, 2));
931+
graph.set_phase(ps[0], Rational64::new(1, 4));
932+
graph.set_phase(ps[1], Rational64::new(1, 2));
933933

934-
let h = g.clone();
934+
let h = graph.clone();
935935

936-
assert!(gadget_fusion(&mut g, gs[0], gs[1]));
937-
assert!(g
938-
.find_vertex(|v| g.phase(v) == Rational::new(3, 4))
936+
assert!(gadget_fusion(&mut graph, gs[0], gs[1]));
937+
assert!(graph
938+
.find_vertex(|v| graph.phase(v) == Rational64::new(3, 4))
939939
.is_some());
940-
assert!(g
941-
.find_vertex(|v| g.phase(v) == Rational::new(1, 4))
940+
assert!(graph
941+
.find_vertex(|v| graph.phase(v) == Rational64::new(1, 4))
942942
.is_none());
943-
assert!(g
944-
.find_vertex(|v| g.phase(v) == Rational::new(1, 2))
943+
assert!(graph
944+
.find_vertex(|v| graph.phase(v) == Rational64::new(1, 2))
945945
.is_none());
946946
// println!("{}", g.to_tensor4());
947947
// println!("{}", h.to_tensor4());
948948
// println!("g = {} * \n {} \n\n", g.scalar(), g.to_dot());
949949
// println!("h = {} * \n {} \n\n", h.scalar(), h.to_dot());
950-
assert_eq!(g.to_tensor4(), h.to_tensor4());
950+
assert_eq!(graph.to_tensor4(), h.to_tensor4());
951951
}
952952
}
953953

954954
#[test]
955955
fn scalar_rules() {
956956
for &t in &[VType::Z, VType::X] {
957957
let mut g = Graph::new();
958-
g.add_vertex_with_phase(t, Rational::new(1, 4));
958+
g.add_vertex_with_phase(t, Rational64::new(1, 4));
959959
let mut h = g.clone();
960960
assert!(remove_single(&mut h, 0));
961961
assert_eq!(h.num_vertices(), 0, "h still has vertices");
@@ -966,8 +966,8 @@ mod tests {
966966
for &t1 in &[VType::Z, VType::X] {
967967
for &et in &[EType::N, EType::H] {
968968
let mut g = Graph::new();
969-
g.add_vertex_with_phase(t0, Rational::new(1, 4));
970-
g.add_vertex_with_phase(t1, Rational::new(-1, 2));
969+
g.add_vertex_with_phase(t0, Rational64::new(1, 4));
970+
g.add_vertex_with_phase(t1, Rational64::new(-1, 2));
971971
g.add_edge_with_type(0, 1, et);
972972
let mut h = g.clone();
973973
assert!(remove_pair(&mut h, 0, 1));

quizx/src/bin/cnot_anneal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2828
let time = Instant::now();
2929
println!("{}", f);
3030
let c = Circuit::from_file(f)
31-
.expect(&format!("circuit failed to parse: {}", f))
31+
.unwrap_or_else(|_| panic!("circuit failed to parse: {}", f))
3232
.to_basic_gates();
3333
println!("...done reading in {:.2?}", time.elapsed());
3434

quizx/src/bin/hidden_shift_stabrank.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
130130
);
131131
if shift == shift_m {
132132
print!("OK {}", data);
133-
fs::write(&format!("hidden_shift_{}_{}_{}", qs, n_ccz, seed), data)
133+
fs::write(format!("hidden_shift_{}_{}_{}", qs, n_ccz, seed), data)
134134
.expect("Unable to write file");
135135
} else {
136136
print!("FAILED {}", data);

quizx/src/bin/pauli_gadget_stabrank.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
183183
check.plug_inputs(&vec![BasisElem::Z0; qs]);
184184
check.plug_outputs(&effect);
185185
let amp = check.to_tensor4()[[]];
186-
let check_prob = &amp * &amp.conj();
186+
let check_prob = amp * amp.conj();
187187
if Scalar::from_scalar(&check_prob) == prob {
188188
println!("OK");
189189
true
@@ -216,7 +216,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
216216
if success {
217217
print!("OK {}", data);
218218
fs::write(
219-
&format!(
219+
format!(
220220
"pauli_gadget_{}_{}_{}_{}_{}_{}",
221221
qs, depth, min_weight, max_weight, nsamples, seed
222222
),

quizx/src/bin/pauli_gadget_stabrank_met.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
184184
check.plug_inputs(&vec![BasisElem::Z0; qs]);
185185
check.plug_outputs(&effect);
186186
let amp = check.to_tensor4()[[]];
187-
let check_prob = &amp * &amp.conj();
187+
let check_prob = amp * amp.conj();
188188
if Scalar::from_scalar(&check_prob) == prob {
189189
println!("OK");
190190
true
@@ -217,7 +217,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
217217
if success {
218218
print!("OK {}", data);
219219
fs::write(
220-
&format!(
220+
format!(
221221
"pauli_gadget_{}_{}_{}_{}_{}_{}",
222222
qs, depth, min_weight, max_weight, nsamples, seed
223223
),

quizx/src/bin/pauli_gadget_stabrank_ne.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
204204
check.plug_inputs(&vec![BasisElem::Z0; qs]);
205205
check.plug_outputs(&effect);
206206
let amp = check.to_tensor4()[[]];
207-
let check_prob = &amp * &amp.conj();
207+
let check_prob = amp * amp.conj();
208208
if Scalar::from_scalar(&check_prob) == prob {
209209
println!("OK");
210210
true
@@ -237,7 +237,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
237237
if success {
238238
print!("OK {}", data);
239239
fs::write(
240-
&format!(
240+
format!(
241241
"pauli_gadget_{}_{}_{}_{}_{}_{}",
242242
qs, depth, min_weight, max_weight, nsamples, seed
243243
),

0 commit comments

Comments
 (0)