File size: 2,355 Bytes
2a87ede | 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 | """
Unit tests for dense_evolution/drawing.py -- draw_circuit.
"""
import pytest
from dense_evolution.drawing import draw_circuit
class TestDrawCircuit:
def test_one_row_per_qubit(self):
diagram = draw_circuit([('h', 0), ('cx', 0, 1)], n_qubits=3)
assert len(diagram.split('\n')) == 3
def test_rows_start_with_qubit_labels(self):
diagram = draw_circuit([], n_qubits=2)
lines = diagram.split('\n')
assert lines[0].startswith('q0:')
assert lines[1].startswith('q1:')
def test_single_qubit_gate_label_appears_on_its_own_row_only(self):
diagram = draw_circuit([('h', 1)], n_qubits=3)
lines = diagram.split('\n')
assert 'H' in lines[1]
assert 'H' not in lines[0]
assert 'H' not in lines[2]
def test_control_and_target_symbols_appear_for_cx(self):
diagram = draw_circuit([('cx', 0, 2)], n_qubits=3)
lines = diagram.split('\n')
assert '*' in lines[0] # control
assert 'X' in lines[2] # target
assert '|' in lines[1] # pass-through on the qubit in between
def test_swap_marks_both_qubits(self):
diagram = draw_circuit([('swap', 0, 1)], n_qubits=2)
lines = diagram.split('\n')
assert 'x' in lines[0] and 'x' in lines[1]
def test_all_rows_have_equal_length(self):
diagram = draw_circuit(
[('h', 0), ('cx', 0, 1), ('rz', 1, 1.23), ('swap', 0, 2)], n_qubits=3)
lines = diagram.split('\n')
assert len({len(line) for line in lines}) == 1
def test_ascii_only_no_unicode_box_drawing(self):
# a printed diagram must survive a plain ASCII/cp1252 console --
# regression guard for the Unicode box-drawing chars this used at
# first, which crash `print()` on a default Windows console.
diagram = draw_circuit(
[('h', 0), ('cx', 0, 1), ('swap', 0, 2), ('ccx', 0, 1, 2)], n_qubits=3)
diagram.encode('ascii') # raises UnicodeEncodeError if not pure ASCII
def test_empty_circuit_still_draws_bare_wires(self):
diagram = draw_circuit([], n_qubits=2)
assert len(diagram.split('\n')) == 2
def test_rejects_fewer_than_one_qubit(self):
with pytest.raises(ValueError):
draw_circuit([], n_qubits=0)
|