|
| 1 | +# mypy: disable-error-code="import-untyped" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from qiskit import QuantumCircuit |
| 6 | + |
| 7 | +from tranqu.transpiler.qiskit_stats_extractor import QiskitStatsExtractor |
| 8 | + |
| 9 | + |
| 10 | +class TestQiskitStatsExtractor: |
| 11 | + def test_simple_circuit_counts(self) -> None: |
| 12 | + """Check that basic 1-qubit / 2-qubit gate counts are correct.""" |
| 13 | + circuit = QuantumCircuit(2) |
| 14 | + circuit.h(0) |
| 15 | + circuit.x(0) |
| 16 | + circuit.cx(0, 1) |
| 17 | + |
| 18 | + extractor = QiskitStatsExtractor() |
| 19 | + stats = extractor.extract_stats_from(circuit) |
| 20 | + |
| 21 | + # Number of qubits |
| 22 | + assert stats["n_qubits"] == 2 |
| 23 | + # Only H, X, and CX (3 operations) are counted as gates |
| 24 | + assert stats["n_gates"] == 3 |
| 25 | + # 1-qubit gates: H and X (2 operations) |
| 26 | + assert stats["n_gates_1q"] == 2 |
| 27 | + # 2-qubit gates: only CX (1 operation) |
| 28 | + assert stats["n_gates_2q"] == 1 |
| 29 | + # Depth should match QuantumCircuit.depth() |
| 30 | + assert stats["depth"] == circuit.depth() |
| 31 | + |
| 32 | + def test_non_gate_operations_are_ignored(self) -> None: |
| 33 | + """Check that non-gate operations.""" |
| 34 | + circuit = QuantumCircuit(2, 2) |
| 35 | + # Gate |
| 36 | + circuit.h(0) |
| 37 | + circuit.x(0) |
| 38 | + circuit.cx(0, 1) |
| 39 | + # Various non-gate operations |
| 40 | + circuit.barrier() |
| 41 | + circuit.measure(0, 0) |
| 42 | + circuit.reset(1) |
| 43 | + circuit.delay(100, 0) |
| 44 | + circuit.initialize([1, 0], 0) |
| 45 | + |
| 46 | + extractor = QiskitStatsExtractor() |
| 47 | + stats = extractor.extract_stats_from(circuit) |
| 48 | + |
| 49 | + # Number of qubits is still correct |
| 50 | + assert stats["n_qubits"] == 2 |
| 51 | + # Only H, X, and CX (3 operations) are counted as gates |
| 52 | + assert stats["n_gates"] == 3 |
| 53 | + assert stats["n_gates_1q"] == 2 |
| 54 | + assert stats["n_gates_2q"] == 1 |
| 55 | + |
| 56 | + def test_empty_circuit(self) -> None: |
| 57 | + """Check that an empty circuit does not raise and all counts are zero.""" |
| 58 | + circuit = QuantumCircuit(3) |
| 59 | + |
| 60 | + extractor = QiskitStatsExtractor() |
| 61 | + stats = extractor.extract_stats_from(circuit) |
| 62 | + |
| 63 | + assert stats["n_qubits"] == 3 |
| 64 | + assert stats["n_gates"] == 0 |
| 65 | + assert stats["n_gates_1q"] == 0 |
| 66 | + assert stats["n_gates_2q"] == 0 |
| 67 | + assert stats["depth"] == 0 |
0 commit comments