1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
//! ZX-calculus optimization for quantum circuits
//!
//! This module implements ZX-calculus, a powerful graphical language for
//! reasoning about quantum computation that enables advanced optimizations
//! through graph rewrite rules.
use crate::builder::Circuit;
use crate::dag::{circuit_to_dag, CircuitDag, DagNode};
use quantrs2_core::{
error::{QuantRS2Error, QuantRS2Result},
gate::GateOp,
qubit::QubitId,
};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::f64::consts::PI;
use std::sync::Arc;
/// A ZX-diagram node representing quantum operations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ZXNode {
/// Green spider (Z-spider) - represents Z-basis operations
ZSpider {
id: usize,
phase: f64,
/// Number of inputs/outputs
arity: usize,
},
/// Red spider (X-spider) - represents X-basis operations
XSpider {
id: usize,
phase: f64,
arity: usize,
},
/// Hadamard gate
Hadamard {
id: usize,
},
/// Input/Output boundaries
Input {
id: usize,
qubit: u32,
},
Output {
id: usize,
qubit: u32,
},
}
impl ZXNode {
#[must_use]
pub const fn id(&self) -> usize {
match self {
Self::ZSpider { id, .. } => *id,
Self::XSpider { id, .. } => *id,
Self::Hadamard { id } => *id,
Self::Input { id, .. } => *id,
Self::Output { id, .. } => *id,
}
}
#[must_use]
pub const fn phase(&self) -> f64 {
match self {
Self::ZSpider { phase, .. } | Self::XSpider { phase, .. } => *phase,
_ => 0.0,
}
}
pub const fn set_phase(&mut self, new_phase: f64) {
match self {
Self::ZSpider { phase, .. } | Self::XSpider { phase, .. } => *phase = new_phase,
_ => {}
}
}
}
/// Edge in ZX-diagram
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ZXEdge {
pub source: usize,
pub target: usize,
/// Hadamard edges are represented as dashed lines in ZX-calculus
pub is_hadamard: bool,
}
/// ZX-diagram representation of a quantum circuit
#[derive(Debug, Clone)]
pub struct ZXDiagram {
/// Nodes in the diagram
pub nodes: HashMap<usize, ZXNode>,
/// Edges between nodes
pub edges: Vec<ZXEdge>,
/// Adjacency list for efficient traversal
pub adjacency: HashMap<usize, Vec<usize>>,
/// Input nodes for each qubit
pub inputs: HashMap<u32, usize>,
/// Output nodes for each qubit
pub outputs: HashMap<u32, usize>,
/// Next available node ID
next_id: usize,
}
impl Default for ZXDiagram {
fn default() -> Self {
Self::new()
}
}
impl ZXDiagram {
/// Create a new empty ZX diagram
#[must_use]
pub fn new() -> Self {
Self {
nodes: HashMap::new(),
edges: Vec::new(),
adjacency: HashMap::new(),
inputs: HashMap::new(),
outputs: HashMap::new(),
next_id: 0,
}
}
/// Add a node to the diagram
pub fn add_node(&mut self, node: ZXNode) -> usize {
let id = self.next_id;
self.next_id += 1;
let node_with_id = match node {
ZXNode::ZSpider { phase, arity, .. } => ZXNode::ZSpider { id, phase, arity },
ZXNode::XSpider { phase, arity, .. } => ZXNode::XSpider { id, phase, arity },
ZXNode::Hadamard { .. } => ZXNode::Hadamard { id },
ZXNode::Input { qubit, .. } => ZXNode::Input { id, qubit },
ZXNode::Output { qubit, .. } => ZXNode::Output { id, qubit },
};
self.nodes.insert(id, node_with_id);
self.adjacency.insert(id, Vec::new());
id
}
/// Add an edge between two nodes
pub fn add_edge(&mut self, source: usize, target: usize, is_hadamard: bool) {
let edge = ZXEdge {
source,
target,
is_hadamard,
};
self.edges.push(edge);
// Update adjacency lists
self.adjacency.entry(source).or_default().push(target);
self.adjacency.entry(target).or_default().push(source);
}
/// Initialize inputs and outputs for a given number of qubits
pub fn initialize_boundaries(&mut self, num_qubits: usize) {
for i in 0..num_qubits {
let qubit = i as u32;
let input_id = self.add_node(ZXNode::Input { id: 0, qubit });
let output_id = self.add_node(ZXNode::Output { id: 0, qubit });
self.inputs.insert(qubit, input_id);
self.outputs.insert(qubit, output_id);
}
}
/// Get neighbors of a node
#[must_use]
pub fn neighbors(&self, node_id: usize) -> &[usize] {
self.adjacency
.get(&node_id)
.map_or(&[], std::vec::Vec::as_slice)
}
/// Apply spider fusion rule
/// Two spiders of the same color connected by a plain edge can be fused
pub fn spider_fusion(&mut self) -> bool {
let mut changed = false;
let mut to_remove = Vec::new();
let mut to_update = Vec::new();
for edge in &self.edges {
if !edge.is_hadamard {
if let (Some(node1), Some(node2)) =
(self.nodes.get(&edge.source), self.nodes.get(&edge.target))
{
// Check if both are spiders of the same type
match (node1, node2) {
(
ZXNode::ZSpider {
id: id1,
phase: phase1,
..
},
ZXNode::ZSpider {
id: id2,
phase: phase2,
..
},
)
| (
ZXNode::XSpider {
id: id1,
phase: phase1,
..
},
ZXNode::XSpider {
id: id2,
phase: phase2,
..
},
) => {
// Fuse the spiders: keep first, remove second
let new_phase = (phase1 + phase2) % (2.0 * PI);
to_update.push((*id1, new_phase));
to_remove.push(*id2);
changed = true;
}
_ => {}
}
}
}
}
// Apply updates
for (id, new_phase) in to_update {
if let Some(node) = self.nodes.get_mut(&id) {
node.set_phase(new_phase);
}
}
// Remove fused nodes and update edges
for id in to_remove {
self.remove_node(id);
}
changed
}
/// Apply identity removal rule
/// A spider with phase 0 and arity 2 can be removed
pub fn identity_removal(&mut self) -> bool {
let mut changed = false;
let mut to_remove = Vec::new();
for (id, node) in &self.nodes {
match node {
ZXNode::ZSpider { phase, arity, .. } | ZXNode::XSpider { phase, arity, .. }
if *arity == 2 && phase.abs() < 1e-10 =>
{
to_remove.push(*id);
}
_ => {}
}
}
for id in to_remove {
// Connect the neighbors directly
let neighbors: Vec<_> = self.neighbors(id).to_vec();
if neighbors.len() == 2 {
self.add_edge(neighbors[0], neighbors[1], false);
changed = true;
}
self.remove_node(id);
}
changed
}
/// π-commutation (Pauli-push) rule: **not currently applied**.
///
/// The π-commutation identity `Z(α)·X(π) = X(π)·Z(-α)` only preserves the
/// diagram's semantics if the π-spider is *relocated* to the other side of
/// the neighbouring spider — a graph edge-surgery, not a local phase tweak.
/// A correct, semantics-preserving implementation requires that relocation
/// (and, in the general entangled case, gflow-aware reasoning), which is not
/// yet implemented here.
///
/// This method therefore deliberately performs **no rewrite** and returns
/// `false` (the honest "nothing changed" signal): it never reports a
/// simplification it did not make, and the other rules
/// ([`spider_fusion`](Self::spider_fusion),
/// [`identity_removal`](Self::identity_removal),
/// [`hadamard_cancellation`](Self::hadamard_cancellation)) already cover the
/// reductions that are sound on the diagrams this module produces. It is
/// kept in the rule set so that adding the real rewrite later is a localized
/// change.
pub const fn pi_commutation(&self) -> bool {
false
}
/// Apply Hadamard cancellation
/// Two adjacent Hadamard gates cancel out
pub fn hadamard_cancellation(&mut self) -> bool {
let mut changed = false;
let mut to_remove = Vec::new();
// Find pairs of adjacent Hadamard nodes
for edge in &self.edges {
if let (Some(ZXNode::Hadamard { id: id1 }), Some(ZXNode::Hadamard { id: id2 })) =
(self.nodes.get(&edge.source), self.nodes.get(&edge.target))
{
// Two Hadamards connected - they cancel out
to_remove.push(*id1);
to_remove.push(*id2);
changed = true;
}
}
for id in to_remove {
self.remove_node(id);
}
changed
}
/// Remove a node and update the graph structure
fn remove_node(&mut self, node_id: usize) {
// Remove from nodes
self.nodes.remove(&node_id);
// Remove from adjacency
self.adjacency.remove(&node_id);
// Remove from other nodes' adjacency lists
for adj_list in self.adjacency.values_mut() {
adj_list.retain(|&id| id != node_id);
}
// Remove edges involving this node
self.edges
.retain(|edge| edge.source != node_id && edge.target != node_id);
}
/// Calculate the T-count (number of T gates) in the diagram
#[must_use]
pub fn t_count(&self) -> usize {
self.nodes
.values()
.filter(|node| {
let phase = node.phase();
(phase - PI / 4.0).abs() < 1e-10
|| (phase - 3.0 * PI / 4.0).abs() < 1e-10
|| (phase - 5.0 * PI / 4.0).abs() < 1e-10
|| (phase - 7.0 * PI / 4.0).abs() < 1e-10
})
.count()
}
/// Apply all optimization rules until convergence
pub fn optimize(&mut self) -> ZXOptimizationResult {
let initial_node_count = self.nodes.len();
let initial_t_count = self.t_count();
let mut iterations = 0;
let max_iterations = 100;
while iterations < max_iterations {
let mut changed = false;
// Apply rewrite rules
changed |= self.spider_fusion();
changed |= self.identity_removal();
changed |= self.hadamard_cancellation();
changed |= self.pi_commutation();
if !changed {
break;
}
iterations += 1;
}
let final_node_count = self.nodes.len();
let final_t_count = self.t_count();
ZXOptimizationResult {
iterations,
initial_node_count,
final_node_count,
initial_t_count,
final_t_count,
converged: iterations < max_iterations,
}
}
}
/// Result of ZX optimization
#[derive(Debug, Clone)]
pub struct ZXOptimizationResult {
pub iterations: usize,
pub initial_node_count: usize,
pub final_node_count: usize,
pub initial_t_count: usize,
pub final_t_count: usize,
pub converged: bool,
}
/// ZX-calculus optimizer
pub struct ZXOptimizer {
/// Maximum number of optimization iterations
pub max_iterations: usize,
/// Enable specific optimization rules
pub enable_spider_fusion: bool,
pub enable_identity_removal: bool,
pub enable_pi_commutation: bool,
pub enable_hadamard_cancellation: bool,
}
impl Default for ZXOptimizer {
fn default() -> Self {
Self {
max_iterations: 100,
enable_spider_fusion: true,
enable_identity_removal: true,
enable_pi_commutation: true,
enable_hadamard_cancellation: true,
}
}
}
impl ZXOptimizer {
/// Create a new ZX optimizer
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Convert a quantum circuit to ZX diagram
pub fn circuit_to_zx<const N: usize>(&self, circuit: &Circuit<N>) -> QuantRS2Result<ZXDiagram> {
let mut diagram = ZXDiagram::new();
diagram.initialize_boundaries(N);
// Track the last node on each qubit wire
let mut qubit_wires = HashMap::new();
for i in 0..N {
let qubit = i as u32;
if let Some(&input_id) = diagram.inputs.get(&qubit) {
qubit_wires.insert(qubit, input_id);
}
}
// Convert each gate to ZX representation
for gate in circuit.gates() {
self.gate_to_zx(gate.as_ref(), &mut diagram, &mut qubit_wires)?;
}
// Connect to outputs
for i in 0..N {
let qubit = i as u32;
if let (Some(&last_node), Some(&output_id)) =
(qubit_wires.get(&qubit), diagram.outputs.get(&qubit))
{
diagram.add_edge(last_node, output_id, false);
}
}
Ok(diagram)
}
/// Convert a single gate to ZX representation
fn gate_to_zx(
&self,
gate: &dyn GateOp,
diagram: &mut ZXDiagram,
qubit_wires: &mut HashMap<u32, usize>,
) -> QuantRS2Result<()> {
let gate_name = gate.name();
let qubits = gate.qubits();
match gate_name {
"H" => {
// Hadamard gate
let qubit = qubits[0].id();
let h_node = diagram.add_node(ZXNode::Hadamard { id: 0 });
if let Some(&prev_node) = qubit_wires.get(&qubit) {
diagram.add_edge(prev_node, h_node, false);
}
qubit_wires.insert(qubit, h_node);
}
"X" => {
// Pauli-X = Z-spider with phase π
let qubit = qubits[0].id();
let x_node = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: PI,
arity: 2,
});
if let Some(&prev_node) = qubit_wires.get(&qubit) {
diagram.add_edge(prev_node, x_node, false);
}
qubit_wires.insert(qubit, x_node);
}
"Y" => {
// Pauli-Y = Z-spider with phase π followed by virtual Z
let qubit = qubits[0].id();
let y_node = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: PI,
arity: 2,
});
if let Some(&prev_node) = qubit_wires.get(&qubit) {
diagram.add_edge(prev_node, y_node, false);
}
qubit_wires.insert(qubit, y_node);
}
"Z" => {
// Pauli-Z = Z-spider with phase π
let qubit = qubits[0].id();
let z_node = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: PI,
arity: 2,
});
if let Some(&prev_node) = qubit_wires.get(&qubit) {
diagram.add_edge(prev_node, z_node, false);
}
qubit_wires.insert(qubit, z_node);
}
"RZ" => {
// Z-rotation = Z-spider with rotation angle
let qubit = qubits[0].id();
// Extract rotation angle from gate properties
let angle = self.extract_rotation_angle(gate);
let rz_node = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: angle,
arity: 2,
});
if let Some(&prev_node) = qubit_wires.get(&qubit) {
diagram.add_edge(prev_node, rz_node, false);
}
qubit_wires.insert(qubit, rz_node);
}
"CNOT" => {
// CNOT = Z-spider on control connected to X-spider on target
let control_qubit = qubits[0].id();
let target_qubit = qubits[1].id();
let control_spider = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: 0.0,
arity: 3,
});
let target_spider = diagram.add_node(ZXNode::XSpider {
id: 0,
phase: 0.0,
arity: 3,
});
// Connect control
if let Some(&prev_control) = qubit_wires.get(&control_qubit) {
diagram.add_edge(prev_control, control_spider, false);
}
// Connect target
if let Some(&prev_target) = qubit_wires.get(&target_qubit) {
diagram.add_edge(prev_target, target_spider, false);
}
// Connect control to target
diagram.add_edge(control_spider, target_spider, false);
qubit_wires.insert(control_qubit, control_spider);
qubit_wires.insert(target_qubit, target_spider);
}
_ => {
// For unsupported gates, add identity spiders
for qubit_id in qubits {
let qubit = qubit_id.id();
let identity_node = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: 0.0,
arity: 2,
});
if let Some(&prev_node) = qubit_wires.get(&qubit) {
diagram.add_edge(prev_node, identity_node, false);
}
qubit_wires.insert(qubit, identity_node);
}
}
}
Ok(())
}
/// Extract the rotation angle of a parameterized single-qubit gate.
///
/// Downcasts the gate to the concrete `core` rotation/phase types and reads
/// the real angle. Returns `0.0` (the identity phase) for gates that carry
/// no angle, so an unrecognized gate contributes a phase-0 spider rather
/// than a fabricated `π/4`.
fn extract_rotation_angle(&self, gate: &dyn GateOp) -> f64 {
use quantrs2_core::gate::single::{Phase, RotationX, RotationY, RotationZ};
let any = gate.as_any();
if let Some(g) = any.downcast_ref::<RotationZ>() {
g.theta
} else if let Some(g) = any.downcast_ref::<RotationX>() {
g.theta
} else if let Some(g) = any.downcast_ref::<RotationY>() {
g.theta
} else if any.downcast_ref::<Phase>().is_some() {
// S gate = Z-rotation by π/2 (up to global phase).
PI / 2.0
} else {
0.0
}
}
/// Optimize a circuit using ZX-calculus.
///
/// The circuit is converted to a ZX diagram, simplified by the rewrite rules
/// to convergence, and extracted back to a circuit. Because circuit
/// extraction from an arbitrary entangled diagram is out of scope (see
/// [`zx_to_circuit`](Self::zx_to_circuit)), the extraction step returns an
/// honest error for diagrams that retain entangling structure (e.g. those
/// containing CNOTs). The `optimization_stats` on the returned result
/// always reflect the *real* diagram-level simplification (node/T-count
/// reductions) regardless of whether extraction succeeds.
pub fn optimize_circuit<const N: usize>(
&self,
circuit: &Circuit<N>,
) -> QuantRS2Result<OptimizedZXResult<N>> {
// Convert to ZX diagram
let mut diagram = self.circuit_to_zx(circuit)?;
// Optimize the diagram
let optimization_result = diagram.optimize();
// Extract a circuit from the simplified diagram (honest error if the
// diagram is not extractable by the linear-wire extractor).
let optimized_circuit = self.zx_to_circuit(&diagram)?;
Ok(OptimizedZXResult {
original_circuit: circuit.clone(),
optimized_circuit,
diagram,
optimization_stats: optimization_result,
})
}
/// Extract a quantum circuit from a ZX diagram.
///
/// General ZX-diagram extraction (recovering a circuit from an arbitrary,
/// entangled, post-optimization diagram) requires gflow-based synthesis and
/// is intentionally out of scope here. This routine performs an **exact**
/// extraction for the class of diagrams that decompose into independent
/// per-qubit wires — i.e. circuits built only from single-qubit gates, plus
/// any diagram the rewrite rules reduce to that form. Each wire is walked
/// from its `Input` to its `Output`, emitting one gate per degree-2 spider /
/// Hadamard encountered.
///
/// If the diagram still contains entangling structure (a spider shared
/// between wires, e.g. a CNOT), this returns an honest
/// [`QuantRS2Error::UnsupportedOperation`] rather than silently dropping the
/// entangling gates and returning a circuit that is *not* equivalent.
fn zx_to_circuit<const N: usize>(&self, diagram: &ZXDiagram) -> QuantRS2Result<Circuit<N>> {
let mut circuit = Circuit::<N>::new();
for qubit in 0..N as u32 {
let Some(&input_id) = diagram.inputs.get(&qubit) else {
continue;
};
let Some(&output_id) = diagram.outputs.get(&qubit) else {
continue;
};
// Walk the wire from the input boundary to the output boundary.
let mut prev = input_id;
let mut current_neighbors = diagram.neighbors(input_id).to_vec();
// An input is degree-1 in a well-formed diagram; follow its single edge.
let mut current = match current_neighbors.as_slice() {
[next] => *next,
[] => continue, // disconnected boundary: nothing on this wire
_ => {
return Err(QuantRS2Error::UnsupportedOperation(format!(
"ZX extraction: input boundary for qubit {qubit} has degree \
{} (expected 1); entangled diagrams are not supported",
current_neighbors.len()
)))
}
};
let mut guard = 0usize;
let node_budget = diagram.nodes.len() + 1;
while current != output_id {
guard += 1;
if guard > node_budget {
return Err(QuantRS2Error::ComputationError(
"ZX extraction: wire traversal did not terminate (cycle in diagram)"
.to_string(),
));
}
let node = diagram.nodes.get(¤t).ok_or_else(|| {
QuantRS2Error::ComputationError(format!(
"ZX extraction: dangling node reference {current}"
))
})?;
current_neighbors = diagram.neighbors(current).to_vec();
// Only degree-2 (pass-through) nodes can be extracted as a wire
// element; higher degree means the node entangles wires.
if current_neighbors.len() != 2 {
return Err(QuantRS2Error::UnsupportedOperation(format!(
"ZX extraction: node {current} on qubit {qubit} has degree {} \
(expected 2); entangling structure cannot be extracted by the \
linear-wire extractor",
current_neighbors.len()
)));
}
// Emit the gate corresponding to this node.
let target = QubitId(qubit);
match node {
ZXNode::ZSpider { phase, .. } => {
emit_phase_gate(&mut circuit, target, *phase, true)?;
}
ZXNode::XSpider { phase, .. } => {
emit_phase_gate(&mut circuit, target, *phase, false)?;
}
ZXNode::Hadamard { .. } => {
circuit.h(target)?;
}
ZXNode::Input { .. } | ZXNode::Output { .. } => {
return Err(QuantRS2Error::ComputationError(format!(
"ZX extraction: unexpected boundary node {current} in wire interior"
)));
}
}
// Step to the neighbor that is not where we came from.
let next = if current_neighbors[0] == prev {
current_neighbors[1]
} else {
current_neighbors[0]
};
prev = current;
current = next;
}
}
Ok(circuit)
}
}
/// Emit the single-qubit gate for a degree-2 spider of the given color.
///
/// A phase of (multiples of) `π` collapses to the corresponding Pauli; `π/2`
/// Z-spiders become `S`; otherwise a parameterized rotation is emitted. A
/// phase-0 spider is the identity and emits nothing.
fn emit_phase_gate<const N: usize>(
circuit: &mut Circuit<N>,
target: QubitId,
phase: f64,
is_z: bool,
) -> QuantRS2Result<()> {
let two_pi = 2.0 * PI;
// Normalize the phase into [0, 2π).
let phase = phase.rem_euclid(two_pi);
if phase.abs() < 1e-10 || (phase - two_pi).abs() < 1e-10 {
return Ok(()); // identity spider
}
if (phase - PI).abs() < 1e-10 {
// Pauli.
if is_z {
circuit.z(target)?;
} else {
circuit.x(target)?;
}
} else if is_z {
circuit.rz(target, phase)?;
} else {
circuit.rx(target, phase)?;
}
Ok(())
}
/// Result of ZX optimization containing original and optimized circuits
#[derive(Debug)]
pub struct OptimizedZXResult<const N: usize> {
pub original_circuit: Circuit<N>,
pub optimized_circuit: Circuit<N>,
pub diagram: ZXDiagram,
pub optimization_stats: ZXOptimizationResult,
}
#[cfg(test)]
mod tests {
use super::*;
use quantrs2_core::gate::multi::CNOT;
use quantrs2_core::gate::single::Hadamard;
#[test]
fn test_zx_diagram_creation() {
let mut diagram = ZXDiagram::new();
diagram.initialize_boundaries(2);
assert_eq!(diagram.inputs.len(), 2);
assert_eq!(diagram.outputs.len(), 2);
}
#[test]
fn test_spider_fusion() {
let mut diagram = ZXDiagram::new();
// Add two Z-spiders with phases π/4 and π/8
let spider1 = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: PI / 4.0,
arity: 2,
});
let spider2 = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: PI / 8.0,
arity: 2,
});
// Connect them
diagram.add_edge(spider1, spider2, false);
// Apply spider fusion
let changed = diagram.spider_fusion();
assert!(changed);
// One spider should be removed
assert_eq!(diagram.nodes.len(), 1);
// Remaining spider should have combined phase
let remaining_node = diagram
.nodes
.values()
.next()
.expect("Expected at least one remaining node after fusion");
assert!((remaining_node.phase() - (PI / 4.0 + PI / 8.0)).abs() < 1e-10);
}
#[test]
fn test_identity_removal() {
let mut diagram = ZXDiagram::new();
// Add identity spider (phase 0, arity 2)
let identity = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: 0.0,
arity: 2,
});
// Add two other nodes
let node1 = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: PI / 4.0,
arity: 2,
});
let node2 = diagram.add_node(ZXNode::ZSpider {
id: 0,
phase: PI / 2.0,
arity: 2,
});
// Connect through identity
diagram.add_edge(node1, identity, false);
diagram.add_edge(identity, node2, false);
let initial_count = diagram.nodes.len();
let changed = diagram.identity_removal();
assert!(changed);
assert_eq!(diagram.nodes.len(), initial_count - 1);
}
#[test]
fn test_circuit_to_zx_conversion() {
let optimizer = ZXOptimizer::new();
let mut circuit = Circuit::<2>::new();
circuit
.add_gate(Hadamard { target: QubitId(0) })
.expect("Failed to add Hadamard gate");
circuit
.add_gate(CNOT {
control: QubitId(0),
target: QubitId(1),
})
.expect("Failed to add CNOT gate");
let diagram = optimizer
.circuit_to_zx(&circuit)
.expect("Failed to convert circuit to ZX diagram");
// Should have input/output nodes plus gate nodes
assert!(diagram.nodes.len() >= 4); // 2 inputs + 2 outputs + gate nodes
assert!(!diagram.edges.is_empty());
}
#[test]
fn test_zx_optimization() {
let optimizer = ZXOptimizer::new();
let mut circuit = Circuit::<1>::new();
circuit
.add_gate(Hadamard { target: QubitId(0) })
.expect("Failed to add first Hadamard gate");
circuit
.add_gate(Hadamard { target: QubitId(0) })
.expect("Failed to add second Hadamard gate"); // Should cancel out
let result = optimizer
.optimize_circuit(&circuit)
.expect("Failed to optimize circuit");
assert!(
result.optimization_stats.final_node_count
<= result.optimization_stats.initial_node_count
);
}
/// `extract_rotation_angle` must read the real gate angle, not a hardcoded
/// `π/4`.
#[test]
fn test_extract_rotation_angle_reads_real_theta() {
use quantrs2_core::gate::single::{RotationX, RotationY, RotationZ};
let optimizer = ZXOptimizer::new();
let rz = RotationZ {
target: QubitId(0),
theta: 0.123,
};
assert!((optimizer.extract_rotation_angle(&rz) - 0.123).abs() < 1e-12);
let rx = RotationX {
target: QubitId(0),
theta: 1.75,
};
assert!((optimizer.extract_rotation_angle(&rx) - 1.75).abs() < 1e-12);
let ry = RotationY {
target: QubitId(0),
theta: -0.6,
};
assert!((optimizer.extract_rotation_angle(&ry) + 0.6).abs() < 1e-12);
// A non-rotation gate must NOT report the bogus π/4.
let h = Hadamard { target: QubitId(0) };
assert!(optimizer.extract_rotation_angle(&h).abs() < 1e-12);
}
/// A single-qubit gate chain must extract back to a non-empty circuit
/// carrying the real gates — not the former empty placeholder circuit.
///
/// We extract directly from the converted diagram (without running the lossy
/// optimize pass) to isolate the extractor: H; RZ(0.4); Z on one wire must
/// come back as H, RZ(0.4), Z. (`circuit_to_zx` encodes a Pauli-Z as a
/// phase-π Z-spider, which the extractor inverts back to a Z gate.)
#[test]
fn test_zx_to_circuit_extracts_single_qubit_chain() {
use quantrs2_core::gate::single::{PauliZ, RotationZ};
let optimizer = ZXOptimizer::new();
let mut circuit = Circuit::<1>::new();
circuit
.add_gate(Hadamard { target: QubitId(0) })
.expect("h");
circuit
.add_gate(RotationZ {
target: QubitId(0),
theta: 0.4,
})
.expect("rz");
circuit.add_gate(PauliZ { target: QubitId(0) }).expect("z");
let diagram = optimizer.circuit_to_zx(&circuit).expect("to zx");
let extracted: Circuit<1> = optimizer.zx_to_circuit(&diagram).expect("extract");
let names: Vec<&str> = extracted.gates().iter().map(|g| g.name()).collect();
// H stays H; RZ(0.4) stays RZ; phase-π Z-spider extracts back to Z.
assert_eq!(names, vec!["H", "RZ", "Z"], "got {names:?}");
// The RZ must carry the real angle (0.4), proving extract_rotation_angle
// and the phase round-trip are real (not a fabricated π/4).
let rz = extracted
.gates()
.iter()
.find(|g| g.name() == "RZ")
.expect("rz present");
let rz_concrete = rz
.as_any()
.downcast_ref::<RotationZ>()
.expect("downcast RZ");
assert!(
(rz_concrete.theta - 0.4).abs() < 1e-10,
"RZ angle {}",
rz_concrete.theta
);
}
/// Extracting a circuit that still contains entangling structure (a CNOT)
/// must return an HONEST error rather than silently dropping the CNOT and
/// returning a non-equivalent circuit.
#[test]
fn test_zx_to_circuit_errors_on_entangling_diagram() {
let optimizer = ZXOptimizer::new();
let mut circuit = Circuit::<2>::new();
circuit
.add_gate(CNOT {
control: QubitId(0),
target: QubitId(1),
})
.expect("cnot");
let result = optimizer.optimize_circuit(&circuit);
assert!(
result.is_err(),
"entangling diagram extraction must error, not fabricate an empty circuit"
);
}
/// An empty single-qubit circuit (or one that cancels to identity) extracts
/// to an empty circuit successfully.
#[test]
fn test_zx_to_circuit_identity_is_empty() {
let optimizer = ZXOptimizer::new();
let mut circuit = Circuit::<1>::new();
circuit
.add_gate(Hadamard { target: QubitId(0) })
.expect("h1");
circuit
.add_gate(Hadamard { target: QubitId(0) })
.expect("h2");
let result = optimizer
.optimize_circuit(&circuit)
.expect("optimize identity");
assert_eq!(result.optimized_circuit.gates().len(), 0);
}
}