|
1 | 1 | import math
|
2 | 2 | from unittest.mock import Mock, call
|
3 | 3 |
|
| 4 | +import cirq |
| 5 | +import numpy as np |
4 | 6 | from kirin import ir
|
5 | 7 |
|
6 |
| -from bloqade import qasm2 |
| 8 | +from bloqade import qasm2, squin |
7 | 9 | from pyqrack.pauli import Pauli
|
| 10 | +from bloqade.pyqrack import StackMemorySimulator |
8 | 11 | from bloqade.pyqrack.base import MockMemory, PyQrackInterpreter
|
9 | 12 |
|
10 | 13 |
|
@@ -91,7 +94,6 @@ def program():
|
91 | 94 |
|
92 | 95 |
|
93 | 96 | def test_basic_control_gates():
|
94 |
| - |
95 | 97 | @qasm2.main
|
96 | 98 | def program():
|
97 | 99 | q = qasm2.qreg(3)
|
@@ -162,3 +164,205 @@ def program():
|
162 | 164 | call.r(3, 0.5, 1),
|
163 | 165 | ]
|
164 | 166 | )
|
| 167 | + |
| 168 | + |
| 169 | +def test_rdm1(): |
| 170 | + """ |
| 171 | + Is extracting the exact state vector consistent with cirq? |
| 172 | + This test also validates the ordering of the qubit basis. |
| 173 | + """ |
| 174 | + |
| 175 | + @squin.kernel |
| 176 | + def program(): |
| 177 | + q = squin.qubit.new(5) |
| 178 | + squin.gate.h(q[1]) |
| 179 | + return q |
| 180 | + |
| 181 | + emulator = StackMemorySimulator(min_qubits=6) |
| 182 | + task = emulator.task(program) |
| 183 | + qubits = task.run() |
| 184 | + rho = emulator.quantum_state(qubits) |
| 185 | + |
| 186 | + assert all(np.isclose(rho.eigenvalues, [1])) |
| 187 | + |
| 188 | + circuit = cirq.Circuit() |
| 189 | + qbs = cirq.LineQubit.range(5) |
| 190 | + circuit.append(cirq.H(qbs[1])) |
| 191 | + for i in range(5): |
| 192 | + circuit.append(cirq.I(qbs[i])) |
| 193 | + state = cirq.Simulator().simulate(circuit).state_vector() |
| 194 | + assert cirq.equal_up_to_global_phase(state, rho[1][:, 0]) |
| 195 | + |
| 196 | + |
| 197 | +def test_rdm1b(): |
| 198 | + """ |
| 199 | + Is extracting the exact state vector consistent with cirq? |
| 200 | + This test also validates the ordering of the qubit basis. |
| 201 | + Same as test_rdm1, but with the total qubits equal to the number of qubits in the program. |
| 202 | + """ |
| 203 | + |
| 204 | + @squin.kernel |
| 205 | + def program(): |
| 206 | + q = squin.qubit.new(5) |
| 207 | + squin.gate.h(q[1]) |
| 208 | + return q |
| 209 | + |
| 210 | + emulator = StackMemorySimulator(min_qubits=5) |
| 211 | + task = emulator.task(program) |
| 212 | + qubits = task.run() |
| 213 | + rho = emulator.quantum_state(qubits) |
| 214 | + |
| 215 | + assert all(np.isclose(rho.eigenvalues, [1])) |
| 216 | + |
| 217 | + circuit = cirq.Circuit() |
| 218 | + qbs = cirq.LineQubit.range(5) |
| 219 | + circuit.append(cirq.H(qbs[1])) |
| 220 | + for i in range(5): |
| 221 | + circuit.append(cirq.I(qbs[i])) |
| 222 | + state = cirq.Simulator().simulate(circuit).state_vector() |
| 223 | + assert cirq.equal_up_to_global_phase(state, rho[1][:, 0]) |
| 224 | + |
| 225 | + |
| 226 | +def test_rdm2(): |
| 227 | + """ |
| 228 | + Does the RDM project correctly? |
| 229 | + """ |
| 230 | + |
| 231 | + @squin.kernel |
| 232 | + def program(): |
| 233 | + """ |
| 234 | + Creates a GHZ state on qubits 0,1,3,4 on a total of 6 qubits. |
| 235 | + """ |
| 236 | + q = squin.qubit.new(6) |
| 237 | + squin.gate.h(q[0]) |
| 238 | + squin.gate.cx(q[0], q[1]) |
| 239 | + squin.gate.cx(q[0], q[3]) |
| 240 | + squin.gate.cx(q[0], q[4]) |
| 241 | + return q |
| 242 | + |
| 243 | + emulator = StackMemorySimulator(min_qubits=6) |
| 244 | + task = emulator.task(program) |
| 245 | + qubits = task.run() |
| 246 | + rho = emulator.quantum_state([qubits[x] for x in [0, 1, 3, 4]]) |
| 247 | + target = np.array([1] + [0] * (14) + [1]) / np.sqrt(2) + 0j |
| 248 | + assert all(np.isclose(rho.eigenvalues, [1])) |
| 249 | + assert cirq.equal_up_to_global_phase(rho[1][:, 0], target) |
| 250 | + |
| 251 | + rho2 = emulator.quantum_state([qubits[x] for x in [0, 1, 3]]) |
| 252 | + assert all(np.isclose(rho2.eigenvalues, [0.5, 0.5])) |
| 253 | + assert rho2.eigenvectors.shape == (8, 2) |
| 254 | + |
| 255 | + |
| 256 | +def test_rdm3(): |
| 257 | + """ |
| 258 | + Out-of-order indexing is consistent with cirq. |
| 259 | + """ |
| 260 | + |
| 261 | + @squin.kernel |
| 262 | + def program(): |
| 263 | + """ |
| 264 | + Random unitaries on 3 qubits. |
| 265 | + """ |
| 266 | + q = squin.qubit.new(3) |
| 267 | + squin.gate.rx(0.1, q[0]) |
| 268 | + squin.gate.ry(0.2, q[1]) |
| 269 | + squin.gate.rx(0.3, q[2]) |
| 270 | + return q |
| 271 | + |
| 272 | + emulator = StackMemorySimulator(min_qubits=6) |
| 273 | + task = emulator.task(program) |
| 274 | + qubits = task.run() |
| 275 | + |
| 276 | + # Canonical ordering |
| 277 | + rho = emulator.quantum_state([qubits[x] for x in [0, 1, 2]]) |
| 278 | + circuit = cirq.Circuit() |
| 279 | + qbs = cirq.LineQubit.range(3) |
| 280 | + circuit.append(cirq.rx(0.1)(qbs[0])) |
| 281 | + circuit.append(cirq.ry(0.2)(qbs[1])) |
| 282 | + circuit.append(cirq.rx(0.3)(qbs[2])) |
| 283 | + state = cirq.Simulator().simulate(circuit).state_vector() |
| 284 | + assert cirq.equal_up_to_global_phase(state, rho[1][:, 0]) |
| 285 | + |
| 286 | + # Reverse ordering 0->2, 1->, 2->0 |
| 287 | + rho = emulator.quantum_state([qubits[x] for x in [2, 1, 0]]) |
| 288 | + circuit = cirq.Circuit() |
| 289 | + qbs = cirq.LineQubit.range(3) |
| 290 | + circuit.append(cirq.rx(0.1)(qbs[2])) |
| 291 | + circuit.append(cirq.ry(0.2)(qbs[1])) |
| 292 | + circuit.append(cirq.rx(0.3)(qbs[0])) |
| 293 | + state = cirq.Simulator().simulate(circuit).state_vector() |
| 294 | + assert cirq.equal_up_to_global_phase(state, rho[1][:, 0]) |
| 295 | + |
| 296 | + # Other ordering |
| 297 | + rho = emulator.quantum_state([qubits[x] for x in [1, 2, 0]]) |
| 298 | + circuit = cirq.Circuit() |
| 299 | + qbs = cirq.LineQubit.range(3) |
| 300 | + circuit.append(cirq.rx(0.1)(qbs[2])) |
| 301 | + circuit.append(cirq.ry(0.2)(qbs[0])) |
| 302 | + circuit.append(cirq.rx(0.3)(qbs[1])) |
| 303 | + state = cirq.Simulator().simulate(circuit).state_vector() |
| 304 | + assert cirq.equal_up_to_global_phase(state, rho[1][:, 0]) |
| 305 | + |
| 306 | + |
| 307 | +def test_rdm4(): |
| 308 | + rho = StackMemorySimulator.quantum_state([]) |
| 309 | + assert rho.eigenvalues.shape == (0,) |
| 310 | + assert rho.eigenvectors.shape == (0, 0) |
| 311 | + |
| 312 | + |
| 313 | +def test_rdm5(): |
| 314 | + @squin.kernel |
| 315 | + def program(): |
| 316 | + """ |
| 317 | + Random unitaries on 3 qubits. |
| 318 | + """ |
| 319 | + q = squin.qubit.new(3) |
| 320 | + return q |
| 321 | + |
| 322 | + emulator = StackMemorySimulator(min_qubits=6) |
| 323 | + task = emulator.task(program) |
| 324 | + qubits = task.run() |
| 325 | + rho = emulator.reduced_density_matrix(qubits) |
| 326 | + assert rho.shape == (8, 8) |
| 327 | + |
| 328 | + |
| 329 | +def test_rdm_failures(): |
| 330 | + @squin.kernel |
| 331 | + def program(): |
| 332 | + q = squin.qubit.new(3) |
| 333 | + return q |
| 334 | + |
| 335 | + emulator = StackMemorySimulator(min_qubits=6) |
| 336 | + task = emulator.task(program) |
| 337 | + qbsA = task.qubits() |
| 338 | + qubits = task.run() |
| 339 | + qubits2 = task.run() |
| 340 | + qbsB = task.qubits() |
| 341 | + assert len(qbsA) == 0 |
| 342 | + assert len(qbsB) == 6 |
| 343 | + |
| 344 | + try: |
| 345 | + emulator.quantum_state([qubits[0], qubits[0]]) |
| 346 | + assert False, "Should have failed; qubits must be unique" |
| 347 | + except ValueError as e: |
| 348 | + assert str(e) == "Qubits must be unique." |
| 349 | + |
| 350 | + try: |
| 351 | + emulator.quantum_state([qubits[0], qubits2[1]]) |
| 352 | + assert False, "Should have failed; qubits must be from the same register" |
| 353 | + except ValueError as e: |
| 354 | + assert str(e) == "All qubits must be from the same simulator register." |
| 355 | + |
| 356 | + |
| 357 | +def test_get_qubits(): |
| 358 | + @squin.kernel |
| 359 | + def program(): |
| 360 | + q = squin.qubit.new(3) |
| 361 | + return q |
| 362 | + |
| 363 | + emulator = StackMemorySimulator(min_qubits=6) |
| 364 | + task = emulator.task(program) |
| 365 | + task.run() |
| 366 | + |
| 367 | + qubits2 = task.qubits() |
| 368 | + assert len(qubits2) == 6 |
0 commit comments