def test_text_conditional_5(self): """ Conditional drawing with 5-bit-length regs.""" qasm_string = """ OPENQASM 2.0; include "qelib1.inc"; qreg q[1]; creg c0[5]; creg c1[5]; if(c0==5) x q[0]; if(c1==5) x q[0]; """ expected = '\n'.join([ " ┌─────┐┌─────┐", "q_0: |0>┤ X ├┤ X ├", " ├──┴──┤└──┬──┘", "c0_0: 0 ╡ ╞═══╪═══", " │ │ │ ", "c0_1: 0 ╡ ╞═══╪═══", " │ │ │ ", "c0_2: 0 ╡ = 5 ╞═══╪═══", " │ │ │ ", "c0_3: 0 ╡ ╞═══╪═══", " │ │ │ ", "c0_4: 0 ╡ ╞═══╪═══", " └─────┘┌──┴──┐", "c1_0: 0 ═══════╡ ╞", " │ │", "c1_1: 0 ═══════╡ ╞", " │ │", "c1_2: 0 ═══════╡ = 5 ╞", " │ │", "c1_3: 0 ═══════╡ ╞", " │ │", "c1_4: 0 ═══════╡ ╞", " └─────┘" ]) circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_pager(self): """ The pager breaks the circuit when the drawing does not fit in the console.""" expected = '\n'.join([ " ┌───┐ »", "q_0: |0>┤ X ├──■──»", " └─┬─┘┌─┴─┐»", "q_1: |0>──■──┤ X ├»", " └───┘»", " c_0: 0 ══════════»", " »", "« ┌─┐┌───┐ »", "«q_0: ┤M├┤ X ├──■──»", "« └╥┘└─┬─┘┌─┴─┐»", "«q_1: ─╫───■──┤ X ├»", "« ║ └───┘»", "«c_0: ═╩═══════════»", "« »", "« ┌─┐┌───┐ ", "«q_0: ┤M├┤ X ├──■──", "« └╥┘└─┬─┘┌─┴─┐", "«q_1: ─╫───■──┤ X ├", "« ║ └───┘", "«c_0: ═╩═══════════", "« " ]) qr = QuantumRegister(2, 'q') cr = ClassicalRegister(1, 'c') circuit = QuantumCircuit(qr, cr) circuit.cx(qr[1], qr[0]) circuit.cx(qr[0], qr[1]) circuit.measure(qr[0], cr[0]) circuit.cx(qr[1], qr[0]) circuit.cx(qr[0], qr[1]) circuit.measure(qr[0], cr[0]) circuit.cx(qr[1], qr[0]) circuit.cx(qr[0], qr[1]) self.assertEqual(str(_text_circuit_drawer(circuit, line_length=20)), expected)
def test_text_measure_2_reverse_bits(self): """ The measure operator, using some registers, with reverse_bits """ expected = '\n'.join([' ┌─┐', 'q2_1: |0>───┤M├', ' ┌─┐└╥┘', 'q2_0: |0>┤M├─╫─', ' └╥┘ ║ ', 'q1_1: |0>─╫──╫─', ' ║ ║ ', 'q1_0: |0>─╫──╫─', ' ║ ║ ', ' c2_1: 0 ═╬══╩═', ' ║ ', ' c2_0: 0 ═╩════', ' ', ' c1_1: 0 ══════', ' ', ' c1_0: 0 ══════', ' ']) qr1 = QuantumRegister(2, 'q1') cr1 = ClassicalRegister(2, 'c1') qr2 = QuantumRegister(2, 'q2') cr2 = ClassicalRegister(2, 'c2') circuit = QuantumCircuit(qr1, qr2, cr1, cr2) circuit.measure(qr2, cr2) self.assertEqual(str(_text_circuit_drawer(circuit, reverse_bits=True)), expected)
def test_text_no_pager(self): """ The pager can be disable.""" qr = QuantumRegister(1, 'q') circuit = QuantumCircuit(qr) for _ in range(100): circuit.h(qr[0]) amount_of_lines = str(_text_circuit_drawer(circuit, line_length=-1)).count('\n') self.assertEqual(amount_of_lines, 2)
def test_text_ch(self): """ ch drawing. """ expected = '\n'.join([ " ┌───┐", "q_0: |0>──■──┤ H ├", " ┌─┴─┐└─┬─┘", "q_1: |0>┤ H ├──┼──", " └───┘ │ ", "q_2: |0>───────■──", " " ]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.ch(qr[0], qr[1]) circuit.ch(qr[2], qr[0]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_measure_html(self): """ The measure operator. HTML representation. """ expected = '\n'.join([ "<pre style=\"word-wrap: normal;" "white-space: pre;line-height: 15px;\"> ┌─┐", "q_0: |0>┤M├", " └╥┘", " c_0: 0 ═╩═", " </pre>" ]) qr = QuantumRegister(1, 'q') cr = ClassicalRegister(1, 'c') circuit = QuantumCircuit(qr, cr) circuit.measure(qr, cr) self.assertEqual(_text_circuit_drawer(circuit)._repr_html_(), expected)
def test_text_cz(self): """ cz drawing. """ expected = '\n'.join([ " ", "q_0: |0>─■──■─", " │ │ ", "q_1: |0>─■──┼─", " │ ", "q_2: |0>────■─", " " ]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.cz(qr[0], qr[1]) circuit.cz(qr[2], qr[0]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_swap(self): """ Swap drawing. """ expected = '\n'.join([ ' ', 'q1_0: |0>─X────', ' │ ', 'q1_1: |0>─┼──X─', ' │ │ ', 'q2_0: |0>─X──┼─', ' │ ', 'q2_1: |0>────X─', ' ' ]) qr1 = QuantumRegister(2, 'q1') qr2 = QuantumRegister(2, 'q2') circuit = QuantumCircuit(qr1, qr2) circuit.swap(qr1, qr2) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_rzz(self): """ rzz drawing. See #1957 """ expected = '\n'.join([ " ", "q_0: |0>─■───────────────────", " │zz(0) ", "q_1: |0>─■───────■───────────", " │zz(1.5708) ", "q_2: |0>─────────■───────────", " " ]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.rzz(0, qr[0], qr[1]) circuit.rzz(pi / 2, qr[2], qr[1]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_sympy_constant(self): """ cu3 drawing with sympy pi""" expected = '\n'.join([ " ", "q_0: |0>──────────■───────────", " ┌─────────┴──────────┐", "q_1: |0>┤ U3(1.5708,pi/2,pi) ├", " └────────────────────┘" ]) qr = QuantumRegister(2, 'q') circuit = QuantumCircuit(qr) circuit.cu3(pi / 2, sympy.pi / 2, sympy.pi, qr[0], qr[1]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_kraus(self): """ Test Kraus. See https://github.com/Qiskit/qiskit-terra/pull/2238#issuecomment-487630014""" expected = '\n'.join( [" ┌───────┐", "q_0: |0>┤ Kraus ├", " └───────┘"]) error = SuperOp(0.75 * numpy.eye(4) + 0.25 * numpy.diag([1, -1, -1, 1])) qr = QuantumRegister(1, name='q') qc = QuantumCircuit(qr) qc.append(error, [qr[0]]) self.assertEqual(str(_text_circuit_drawer(qc)), expected)
def test_text_parameters_mix(self): """ cu3 drawing with parameters""" expected = '\n'.join([" ", "q_0: |0>─────────────■─────────────", " ┌────────────┴────────────┐", "q_1: |0>┤ U3(1.5708,theta,3.1416) ├", " └─────────────────────────┘"]) qr = QuantumRegister(2, 'q') circuit = QuantumCircuit(qr) circuit.cu3(pi / 2, Parameter('theta'), pi, qr[0], qr[1]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_spacing_2378(self): """Small gates in the same layer as long gates. See https://github.com/Qiskit/qiskit-terra/issues/2378""" expected = '\n'.join([ " ", "q_0: |0>──────X──────", " │ ", "q_1: |0>──────X──────", " ┌───────────┐", "q_2: |0>┤ Rz(11111) ├", " └───────────┘" ]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.swap(qr[0], qr[1]) circuit.rz(11111, qr[2]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_justify_right_barrier(self): """ Right justify respects barriers """ expected = '\n'.join([' ┌───┐ ░ ', 'q1_0: |0>┤ H ├─░──────', ' └───┘ ░ ┌───┐', 'q1_1: |0>──────░─┤ H ├', ' ░ └───┘']) qr1 = QuantumRegister(2, 'q1') circuit = QuantumCircuit(qr1) circuit.h(qr1[0]) circuit.barrier(qr1) circuit.h(qr1[1]) self.assertEqual(str(_text_circuit_drawer(circuit, justify='right')), expected)
def test_text_crz(self): """ crz drawing. """ expected = '\n'.join([" ┌────────────┐", "q_0: |0>──────■───────┤ Rz(1.5708) ├", " ┌─────┴──────┐└─────┬──────┘", "q_1: |0>┤ Rz(1.5708) ├──────┼───────", " └────────────┘ │ ", "q_2: |0>────────────────────■───────", " "]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.crz(pi / 2, qr[0], qr[1]) circuit.crz(pi / 2, qr[2], qr[0]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_overlap_swap(self): """ Swap is drawn in 2 separate columns""" expected = '\n'.join([ ' ', 'q1_0: |0>─X────', ' │ ', 'q1_1: |0>─┼──X─', ' │ │ ', 'q2_0: |0>─X──┼─', ' │ ', 'q2_1: |0>────X─', ' ' ]) qr1 = QuantumRegister(2, 'q1') qr2 = QuantumRegister(2, 'q2') circuit = QuantumCircuit(qr1, qr2) circuit.swap(qr1, qr2) self.assertEqual(str(_text_circuit_drawer(circuit, justify='left')), expected)
def test_text_barrier(self): """ Barrier drawing. """ expected = '\n'.join([ ' ░ ', 'q1_0: |0>─░─', ' ░ ', 'q1_1: |0>─░─', ' ░ ', 'q2_0: |0>───', ' ░ ', 'q2_1: |0>─░─', ' ░ ' ]) qr1 = QuantumRegister(2, 'q1') qr2 = QuantumRegister(2, 'q2') circuit = QuantumCircuit(qr1, qr2) circuit.barrier(qr1) circuit.barrier(qr2[1]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_single_gate(self): """ Single Qbit gate drawing. """ expected = '\n'.join([ ' ┌───┐', 'q1_0: |0>┤ H ├', ' ├───┤', 'q1_1: |0>┤ H ├', ' └───┘', 'q2_0: |0>─────', ' ┌───┐', 'q2_1: |0>┤ H ├', ' └───┘' ]) qr1 = QuantumRegister(2, 'q1') qr2 = QuantumRegister(2, 'q2') circuit = QuantumCircuit(qr1, qr2) circuit.h(qr1) circuit.h(qr2[1]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_reset(self): """ Reset drawing. """ expected = '\n'.join([ ' ', 'q1_0: |0>─|0>─', ' ', 'q1_1: |0>─|0>─', ' ', 'q2_0: |0>─────', ' ', 'q2_1: |0>─|0>─', ' ' ]) qr1 = QuantumRegister(2, 'q1') qr2 = QuantumRegister(2, 'q2') circuit = QuantumCircuit(qr1, qr2) circuit.reset(qr1) circuit.reset(qr2[1]) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_cu1_reverse_bits(self): """ cu1 drawing with reverse_bits""" expected = '\n'.join([ " ", "q_2: |0>──────────■───────", " │ ", "q_1: |0>─■────────┼───────", " │1.5708 │1.5708 ", "q_0: |0>─■────────■───────", " " ]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.cu1(pi / 2, qr[0], qr[1]) circuit.cu1(pi / 2, qr[2], qr[0]) self.assertEqual(str(_text_circuit_drawer(circuit, reverse_bits=True)), expected)
def test_text_cswap_reverse_bits(self): """ CSwap drawing with reverse_bits. """ expected = '\n'.join([ ' ', 'q_2: |0>─X──X──■─', ' │ │ │ ', 'q_1: |0>─X──■──X─', ' │ │ │ ', 'q_0: |0>─■──X──X─', ' ' ]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.cswap(qr[0], qr[1], qr[2]) circuit.cswap(qr[1], qr[0], qr[2]) circuit.cswap(qr[2], qr[1], qr[0]) self.assertEqual(str(_text_circuit_drawer(circuit, reverse_bits=True)), expected)
def test_text_overlap_measure(self): """ Measure is drawn not overlapping""" expected = '\n'.join([ ' ┌─┐ ', 'q1_0: |0>┤M├─────', ' └╥┘┌───┐', 'q1_1: |0>─╫─┤ X ├', ' ║ └───┘', ' c1_0: 0 ═╩══════', ' ', ' c1_1: 0 ════════', ' ' ]) qr1 = QuantumRegister(2, 'q1') cr1 = ClassicalRegister(2, 'c1') circuit = QuantumCircuit(qr1, cr1) circuit.measure(qr1[0], cr1[0]) circuit.x(qr1[1]) self.assertEqual(str(_text_circuit_drawer(circuit, justify='left')), expected)
def test_text_overlap_cx(self): """ Overlapping CX gates are drawn not overlapping""" expected = '\n'.join([ ' ', 'q1_0: |0>──■───────', ' │ ', 'q1_1: |0>──┼────■──', ' │ ┌─┴─┐', 'q1_2: |0>──┼──┤ X ├', ' ┌─┴─┐└───┘', 'q1_3: |0>┤ X ├─────', ' └───┘ ' ]) qr1 = QuantumRegister(4, 'q1') circuit = QuantumCircuit(qr1) circuit.cx(qr1[0], qr1[3]) circuit.cx(qr1[1], qr1[2]) self.assertEqual(str(_text_circuit_drawer(circuit, justify='left')), expected)
def test_text_cu3_reverse_bits(self): """ cu3 drawing with reverse_bits""" expected = '\n'.join([" ", "q_2: |0>─────────────────────────────────────────■──────────────", " ┌──────────────────────────┐ │ ", "q_1: |0>┤ U3(1.5708,1.5708,1.5708) ├─────────────┼──────────────", " └────────────┬─────────────┘┌────────────┴─────────────┐", "q_0: |0>─────────────■──────────────┤ U3(1.5708,1.5708,1.5708) ├", " └──────────────────────────┘"]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.cu3(pi / 2, pi / 2, pi / 2, qr[0], qr[1]) circuit.cu3(pi / 2, pi / 2, pi / 2, qr[2], qr[0]) self.assertEqual(str(_text_circuit_drawer(circuit, reverse_bits=True)), expected)
def test_2Qgate_nottogether(self): """ 2Q that are not together """ expected = '\n'.join([ " ┌───────┐", "q_2: |0>┤1 ├", " │ │", "q_1: |0>┤ twoQ ├", " │ │", "q_0: |0>┤0 ├", " └───────┘" ]) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) my_gate2 = Gate(name='twoQ', num_qubits=2, params=[]) circuit.append(my_gate2, [qr[0], qr[2]]) self.assertEqual(str(_text_circuit_drawer(circuit, reverse_bits=True)), expected)
def test_2Qgate_cross_wires(self): """ 2Q no params, with cross wires """ expected = '\n'.join([ " ┌───────┐", "q_1: |0>┤0 ├", " │ twoQ │", "q_0: |0>┤1 ├", " └───────┘" ]) qr = QuantumRegister(2, 'q') circuit = QuantumCircuit(qr) my_gate2 = Gate(name='twoQ', num_qubits=2, params=[]) circuit.append(my_gate2, [qr[1], qr[0]]) self.assertEqual(str(_text_circuit_drawer(circuit, reverse_bits=True)), expected)
def test_text_measure_1(self): """ The measure operator, using 3-bit-length registers. """ expected = '\n'.join([ ' ┌─┐ ', 'q_0: |0>┤M├──────', ' └╥┘┌─┐ ', 'q_1: |0>─╫─┤M├───', ' ║ └╥┘┌─┐', 'q_2: |0>─╫──╫─┤M├', ' ║ ║ └╥┘', ' c_0: 0 ═╩══╬══╬═', ' ║ ║ ', ' c_1: 0 ════╩══╬═', ' ║ ', ' c_2: 0 ═══════╩═', ' ' ]) qr = QuantumRegister(3, 'q') cr = ClassicalRegister(3, 'c') circuit = QuantumCircuit(qr, cr) circuit.measure(qr, cr) self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
def test_text_justify_right_measure_resize(self): """ Measure gate can resize if necessary""" expected = '\n'.join([ ' ┌───┐', 'q1_0: |0>┤ X ├', ' └┬─┬┘', 'q1_1: |0>─┤M├─', ' └╥┘ ', ' c1_0: 0 ══╬══', ' ║ ', ' c1_1: 0 ══╩══', ' ' ]) qr1 = QuantumRegister(2, 'q1') cr1 = ClassicalRegister(2, 'c1') circuit = QuantumCircuit(qr1, cr1) circuit.x(qr1[0]) circuit.measure(qr1[1], cr1[1]) self.assertEqual(str(_text_circuit_drawer(circuit, justify='right')), expected)
def test_text_drawer_cp437(self): filename = self._get_resource_path('current_textplot_cp437.txt') qc = self.sample_circuit() output = _text_circuit_drawer(qc, filename=filename, fold=-1, initial_state=True, cregbundle=False, encoding='cp437') try: encode(str(output), encoding='cp437') except UnicodeEncodeError: self.fail("_text_circuit_drawer() should be cp437.") self.assertFilesAreEqual(filename, self.text_reference_cp437, 'cp437') os.remove(filename)
def test_text_drawer_utf8(self): filename = 'current_textplot_utf8.txt' qc = self.sample_circuit() output = _text_circuit_drawer(qc, filename=filename, fold=-1, initial_state=True, cregbundle=False, encoding='utf8') try: encode(str(output), encoding='utf8') except UnicodeEncodeError: self.fail("_text_circuit_drawer() should be utf8.") self.assertFilesAreEqual(filename, self.text_reference_utf8, 'utf8') os.remove(filename)