|
| 1 | +@testitem "Traversal" tags=[:traversal] begin |
| 2 | + |
| 3 | +using PBCCompiler |
| 4 | +using PBCCompiler: Circuit, CircuitOp, Pauli, Measurement, ExpHalfPiPauli, traversal |
| 5 | +using QuantumClifford: @P_str |
| 6 | +using Moshi.Data: isa_variant |
| 7 | + |
| 8 | +@testset "Basic traversal" begin |
| 9 | + # Test empty circuit |
| 10 | + circuit = Circuit() |
| 11 | + traversal(circuit, (a, b) -> nothing) |
| 12 | + @test isempty(circuit) |
| 13 | + |
| 14 | + # Test single-element circuit (no pairs to traverse) |
| 15 | + circuit = Circuit([Pauli(P"X", [1])]) |
| 16 | + traversal(circuit, (a, b) -> nothing) |
| 17 | + @test length(circuit) == 1 |
| 18 | +end |
| 19 | + |
| 20 | +@testset "Swap transformation" begin |
| 21 | + # Simple swap: swap adjacent operations |
| 22 | + swap_transform(op1, op2) = (op2, op1) |
| 23 | + |
| 24 | + # Create circuit [X, Y, Z] |
| 25 | + circuit = Circuit([ |
| 26 | + Pauli(P"X", [1]), |
| 27 | + Pauli(P"Y", [1]), |
| 28 | + Pauli(P"Z", [1]) |
| 29 | + ]) |
| 30 | + |
| 31 | + # After traversal with swap: |
| 32 | + # Pair (X, Y) -> (Y, X): [Y, X, Z] |
| 33 | + # Pair (X, Z) -> (Z, X): [Y, Z, X] |
| 34 | + traversal(circuit, swap_transform) |
| 35 | + |
| 36 | + @test isa_variant(circuit[1], CircuitOp.Pauli) |
| 37 | + @test isa_variant(circuit[2], CircuitOp.Pauli) |
| 38 | + @test isa_variant(circuit[3], CircuitOp.Pauli) |
| 39 | + # Check that X has moved to the end (bubble sort behavior) |
| 40 | + @test circuit[1].pauli == P"Y" |
| 41 | + @test circuit[2].pauli == P"Z" |
| 42 | + @test circuit[3].pauli == P"X" |
| 43 | +end |
| 44 | + |
| 45 | +@testset "No-op transformation" begin |
| 46 | + # Transformation that returns nothing (no change) |
| 47 | + no_op(op1, op2) = nothing |
| 48 | + |
| 49 | + circuit = Circuit([ |
| 50 | + Pauli(P"X", [1]), |
| 51 | + Pauli(P"Y", [1]), |
| 52 | + Pauli(P"Z", [1]) |
| 53 | + ]) |
| 54 | + |
| 55 | + original_length = length(circuit) |
| 56 | + traversal(circuit, no_op) |
| 57 | + |
| 58 | + @test length(circuit) == original_length |
| 59 | + @test circuit[1].pauli == P"X" |
| 60 | + @test circuit[2].pauli == P"Y" |
| 61 | + @test circuit[3].pauli == P"Z" |
| 62 | +end |
| 63 | + |
| 64 | +@testset "Combining transformation" begin |
| 65 | + # Transformation that combines two operations into one |
| 66 | + # For testing, combine any two Paulis into a single X |
| 67 | + combine_paulis(op1, op2) = begin |
| 68 | + if isa_variant(op1, CircuitOp.Pauli) && isa_variant(op2, CircuitOp.Pauli) |
| 69 | + return Pauli(P"X", [1]) # Combine into single X |
| 70 | + end |
| 71 | + return nothing |
| 72 | + end |
| 73 | + |
| 74 | + circuit = Circuit([ |
| 75 | + Pauli(P"X", [1]), |
| 76 | + Pauli(P"Y", [1]), |
| 77 | + Pauli(P"Z", [1]) |
| 78 | + ]) |
| 79 | + |
| 80 | + # After first combination: [X, Z] (X and Y combined into X) |
| 81 | + # After second combination: [X] (X and Z combined into X) |
| 82 | + traversal(circuit, combine_paulis) |
| 83 | + |
| 84 | + @test length(circuit) == 1 |
| 85 | + @test isa_variant(circuit[1], CircuitOp.Pauli) |
| 86 | +end |
| 87 | + |
| 88 | +@testset "Left direction traversal" begin |
| 89 | + swap_transform(op1, op2) = (op2, op1) |
| 90 | + |
| 91 | + circuit = Circuit([ |
| 92 | + Pauli(P"X", [1]), |
| 93 | + Pauli(P"Y", [1]), |
| 94 | + Pauli(P"Z", [1]) |
| 95 | + ]) |
| 96 | + |
| 97 | + # With left direction, start from the right side |
| 98 | + # Pair (Y, Z) -> (Z, Y): [X, Z, Y] |
| 99 | + # Pair (X, Z) -> (Z, X): [Z, X, Y] |
| 100 | + traversal(circuit, swap_transform, :left) |
| 101 | + |
| 102 | + @test circuit[1].pauli == P"Z" |
| 103 | + @test circuit[2].pauli == P"X" |
| 104 | + @test circuit[3].pauli == P"Y" |
| 105 | +end |
| 106 | + |
| 107 | +@testset "Partial traversal with indices" begin |
| 108 | + swap_transform(op1, op2) = (op2, op1) |
| 109 | + |
| 110 | + circuit = Circuit([ |
| 111 | + Pauli(P"X", [1]), |
| 112 | + Pauli(P"Y", [1]), |
| 113 | + Pauli(P"Z", [1]), |
| 114 | + Pauli(P"I", [1]) |
| 115 | + ]) |
| 116 | + |
| 117 | + # Only traverse from index 2 to 2 (pair at positions 2,3) |
| 118 | + traversal(circuit, swap_transform, :right, 2, 2) |
| 119 | + |
| 120 | + # Only Y and Z should be swapped |
| 121 | + @test circuit[1].pauli == P"X" |
| 122 | + @test circuit[2].pauli == P"Z" |
| 123 | + @test circuit[3].pauli == P"Y" |
| 124 | + @test circuit[4].pauli == P"I" |
| 125 | +end |
| 126 | + |
| 127 | +@testset "Conditional transformation" begin |
| 128 | + # Only swap if first operation is an X Pauli |
| 129 | + conditional_swap(op1, op2) = begin |
| 130 | + if isa_variant(op1, CircuitOp.Pauli) && op1.pauli == P"X" |
| 131 | + return (op2, op1) |
| 132 | + end |
| 133 | + return nothing |
| 134 | + end |
| 135 | + |
| 136 | + circuit = Circuit([ |
| 137 | + Pauli(P"X", [1]), |
| 138 | + Pauli(P"Y", [1]), |
| 139 | + Pauli(P"Z", [1]) |
| 140 | + ]) |
| 141 | + |
| 142 | + # Pair (X, Y): X is first, so swap -> [Y, X, Z] |
| 143 | + # Pair (X, Z): X is first, so swap -> [Y, Z, X] |
| 144 | + traversal(circuit, conditional_swap) |
| 145 | + |
| 146 | + @test circuit[1].pauli == P"Y" |
| 147 | + @test circuit[2].pauli == P"Z" |
| 148 | + @test circuit[3].pauli == P"X" |
| 149 | +end |
| 150 | + |
| 151 | +@testset "Invalid direction error" begin |
| 152 | + circuit = Circuit([Pauli(P"X", [1]), Pauli(P"Y", [1])]) |
| 153 | + @test_throws ArgumentError traversal(circuit, (a,b) -> nothing, :invalid) |
| 154 | +end |
| 155 | + |
| 156 | +end |
0 commit comments