|
| 1 | +"""Tests for basic permutation functionality in both QASM and QIR generation.""" |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pecos.slr import CReg, Main, Permute, SlrConverter |
| 7 | + |
| 8 | +# Test fixtures |
| 9 | + |
| 10 | + |
| 11 | +def create_basic_permutation_program(): |
| 12 | + """Create a basic program with permutation of classical registers.""" |
| 13 | + a = CReg("a", 2) |
| 14 | + b = CReg("b", 2) |
| 15 | + |
| 16 | + prog = Main( |
| 17 | + a, |
| 18 | + b, |
| 19 | + Permute( |
| 20 | + [a[0], b[1]], |
| 21 | + [b[1], a[0]], |
| 22 | + ), |
| 23 | + a[0].set(1), # Should become b[1] = 1 after permutation |
| 24 | + ) |
| 25 | + |
| 26 | + return prog, a, b |
| 27 | + |
| 28 | + |
| 29 | +def create_same_register_permutation_program(): |
| 30 | + """Create a program with permutation within the same register.""" |
| 31 | + a = CReg("a", 3) |
| 32 | + |
| 33 | + prog = Main( |
| 34 | + a, |
| 35 | + Permute( |
| 36 | + [a[0], a[1], a[2]], |
| 37 | + [a[2], a[0], a[1]], |
| 38 | + ), |
| 39 | + a[0].set(1), # Should become a[2] = 1 |
| 40 | + a[1].set(0), # Should become a[0] = 0 |
| 41 | + a[2].set(1), # Should become a[1] = 1 |
| 42 | + ) |
| 43 | + |
| 44 | + return prog, a |
| 45 | + |
| 46 | + |
| 47 | +# QASM Tests |
| 48 | + |
| 49 | + |
| 50 | +def test_permutation_consistency_for_bits_in_qasm(): |
| 51 | + """Test that permutation is consistent across multiple QASM generations.""" |
| 52 | + prog = Main( |
| 53 | + a := CReg("a", 2), |
| 54 | + b := CReg("b", 2), |
| 55 | + Permute( |
| 56 | + [a[0], b[1]], |
| 57 | + [b[1], a[0]], |
| 58 | + ), |
| 59 | + a[0].set(1), |
| 60 | + ) |
| 61 | + |
| 62 | + qasm1 = SlrConverter(prog).qasm() |
| 63 | + qasm2 = SlrConverter(prog).qasm() |
| 64 | + |
| 65 | + # Print the QASM for debugging |
| 66 | + print("\nQASM output:") |
| 67 | + print(qasm1) |
| 68 | + |
| 69 | + assert qasm1 == qasm2 |
| 70 | + assert "a[0] = 1;" in qasm1 |
| 71 | + |
| 72 | + # Verify that the bit permutation is using the temporary bit approach, not XOR swap |
| 73 | + assert "creg _bit_swap[1];" in qasm1 |
| 74 | + assert "_bit_swap[0] = a[0];" in qasm1 |
| 75 | + assert "a[0] = b[1];" in qasm1 |
| 76 | + assert "b[1] = _bit_swap[0];" in qasm1 |
| 77 | + assert "a[0] = a[0] ^ b[1];" not in qasm1 # Make sure XOR swap is not used |
| 78 | + |
| 79 | + |
| 80 | +def test_basic_permutation_qasm(basic_permutation_program): |
| 81 | + """Test basic permutation functionality in QASM generation.""" |
| 82 | + prog, _, _ = basic_permutation_program |
| 83 | + |
| 84 | + # Generate QASM |
| 85 | + from pecos.slr.gen_codes.gen_qasm import QASMGenerator |
| 86 | + from pecos.slr.slr_converter import SlrConverter |
| 87 | + |
| 88 | + # Create a custom QASM generator to debug the permutation map |
| 89 | + generator = QASMGenerator() |
| 90 | + generator.generate_block(prog) |
| 91 | + qasm = generator.get_output() |
| 92 | + |
| 93 | + # Print the QASM for debugging |
| 94 | + print("\nQASM output:") |
| 95 | + print(qasm) |
| 96 | + |
| 97 | + # Print the permutation map |
| 98 | + print("\nPermutation map:") |
| 99 | + print(generator.permutation_map) |
| 100 | + |
| 101 | + # Verify that the QASM contains the correct permuted operation |
| 102 | + # For classical bit permutations, operations still refer to the original bit names |
| 103 | + assert "a[0] = 1;" in qasm |
| 104 | + |
| 105 | + # Verify that the bit permutation is using the temporary bit approach, not XOR swap |
| 106 | + assert "creg _bit_swap[1];" in qasm |
| 107 | + assert "_bit_swap[0] = a[0];" in qasm |
| 108 | + assert "a[0] = b[1];" in qasm |
| 109 | + assert "b[1] = _bit_swap[0];" in qasm |
| 110 | + assert "a[0] = a[0] ^ b[1];" not in qasm # Make sure XOR swap is not used |
| 111 | + |
| 112 | + # Verify that running QASM generation twice produces consistent results |
| 113 | + qasm2 = SlrConverter(prog).qasm() |
| 114 | + assert qasm == qasm2, "QASM generation is not deterministic" |
| 115 | + |
| 116 | + |
| 117 | +def test_same_register_permutation_qasm(same_register_permutation_program): |
| 118 | + """Test permutation of elements within the same register in QASM.""" |
| 119 | + prog, _ = same_register_permutation_program |
| 120 | + |
| 121 | + qasm = SlrConverter(prog).qasm() |
| 122 | + |
| 123 | + # Print the QASM for debugging |
| 124 | + print("\nQASM output:") |
| 125 | + print(qasm) |
| 126 | + |
| 127 | + # For classical bit permutations, operations still refer to the original bit names |
| 128 | + assert "a[0] = 1;" in qasm |
| 129 | + assert "a[1] = 0;" in qasm |
| 130 | + assert "a[2] = 1;" in qasm |
| 131 | + |
| 132 | + # Verify that the bit permutation is using the temporary bit approach, not XOR swap |
| 133 | + assert "creg _bit_swap[1];" in qasm |
| 134 | + assert "_bit_swap[0] = a[0];" in qasm |
| 135 | + assert "a[0] = a[2];" in qasm # Part of the cycle |
| 136 | + assert "a[2] = a[1];" in qasm # Part of the cycle |
| 137 | + assert "a[1] = _bit_swap[0];" in qasm # Completing the cycle |
| 138 | + assert "a[0] = a[0] ^ a[1];" not in qasm # Make sure XOR swap is not used |
| 139 | + |
| 140 | + |
| 141 | +# QIR Tests |
| 142 | + |
| 143 | + |
| 144 | +@pytest.mark.optional_dependency |
| 145 | +def test_basic_permutation_qir(basic_permutation_program): |
| 146 | + """Test basic permutation functionality in QIR generation.""" |
| 147 | + prog, _, _ = basic_permutation_program |
| 148 | + |
| 149 | + # Generate QIR |
| 150 | + qir = SlrConverter(prog).qir() |
| 151 | + |
| 152 | + # Print the QIR for debugging |
| 153 | + print("\nQIR output:") |
| 154 | + print(qir) |
| 155 | + |
| 156 | + # Verify that the QIR contains a comment about the permutation |
| 157 | + assert "Permutation: a[0] -> b[1], b[1] -> a[0]" in qir |
| 158 | + |
| 159 | + # Extract the register and index used in the set_creg_bit call |
| 160 | + # This should be setting a[0] (register %a, index 0) since the permutation |
| 161 | + # is not being applied to the operations in the QIR generator |
| 162 | + set_creg_calls = re.findall( |
| 163 | + r"call void @set_creg_bit\(i1\* %(\w+), i64 (\d+), i1 1\)", |
| 164 | + qir, |
| 165 | + ) |
| 166 | + |
| 167 | + # We should have at least one set_creg_bit call |
| 168 | + assert len(set_creg_calls) >= 1, "No set_creg_bit call found" |
| 169 | + |
| 170 | + # Get the register and index |
| 171 | + reg_name, index = set_creg_calls[0] |
| 172 | + |
| 173 | + # Verify that the set_creg_bit call is setting a[0] since the permutation |
| 174 | + # is not being applied to the operations in the QIR generator |
| 175 | + assert reg_name == "a", f"set_creg_bit applied to register {reg_name}, expected a" |
| 176 | + assert index == "0", f"set_creg_bit applied to index {index}, expected 0" |
| 177 | + |
| 178 | + # Verify that running QIR generation twice produces consistent results |
| 179 | + qir2 = SlrConverter(prog).qir() |
| 180 | + assert qir == qir2, "QIR generation is not deterministic" |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.optional_dependency |
| 184 | +def test_same_register_permutation_qir(same_register_permutation_program): |
| 185 | + """Test permutation of elements within the same register in QIR.""" |
| 186 | + prog, _ = same_register_permutation_program |
| 187 | + |
| 188 | + qir = SlrConverter(prog).qir() |
| 189 | + |
| 190 | + # Print the QIR for debugging |
| 191 | + print("\nQIR output:") |
| 192 | + print(qir) |
| 193 | + |
| 194 | + # Verify that the QIR contains a comment about the permutation |
| 195 | + assert "Permutation: a[0] -> a[2], a[1] -> a[0], a[2] -> a[1]" in qir |
| 196 | + |
| 197 | + # Extract the register and indices used in the set_creg_bit calls |
| 198 | + set_creg_calls = re.findall( |
| 199 | + r"call void @set_creg_bit\(i1\* %(\w+), i64 (\d+), i1 (\d+)\)", |
| 200 | + qir, |
| 201 | + ) |
| 202 | + |
| 203 | + # We should have at least three set_creg_bit calls |
| 204 | + assert ( |
| 205 | + len(set_creg_calls) >= 3 |
| 206 | + ), f"Expected at least 3 set_creg_bit calls, found {len(set_creg_calls)}" |
| 207 | + |
| 208 | + # Create a dictionary to store the values set for each index |
| 209 | + set_values = {} |
| 210 | + for reg_name, index, value in set_creg_calls: |
| 211 | + assert ( |
| 212 | + reg_name == "a" |
| 213 | + ), f"set_creg_bit applied to register {reg_name}, expected a" |
| 214 | + set_values[int(index)] = int(value) |
| 215 | + |
| 216 | + # Verify that the set_creg_bit calls are setting the correct values |
| 217 | + # Since the permutation is not being applied to the operations in the QIR generator, |
| 218 | + # we expect the original operations to be executed |
| 219 | + assert set_values.get(0) == 1, "a[0] should be set to 1" |
| 220 | + assert set_values.get(1) == 0, "a[1] should be set to 0" |
| 221 | + assert set_values.get(2) == 1, "a[2] should be set to 1" |
0 commit comments