Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def test_text_measure_2_reversebits(self):
        """ The measure operator, using some registers, with reversebits """
        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, reversebits=True)), expected)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 def test_text_unitary_1q(self):
     """Test unitary matrix gate on 1 qubit"""
     qr = QuantumRegister(1, 'q')
     cr = ClassicalRegister(1, 'c')
     circuit = QuantumCircuit(qr, cr)
     circuit.unitary([[1, 0], [0, 1]], qr[0])
     actual = str(_text_circuit_drawer(circuit))
     expected = '\n'.join([
         '        ┌─────────┐', 'q_0: |0>┤ Unitary ├',
         '        └─────────┘', ' c_0: 0 ═══════════', '                   '
     ])
     self.assertEqual(actual, expected)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
 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_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)
Ejemplo n.º 12
0
 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_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_swap_reversebits(self):
        """ Swap drawing with reversebits. """
        expected = '\n'.join([
            "               ", "q2_1: |0>─X────", "          │    ",
            "q2_0: |0>─┼──X─", "          │  │ ", "q1_1: |0>─X──┼─",
            "             │ ", "q1_0: |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, reversebits=True)),
                         expected)
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
 def test_text_rzz(self):
     """ rzz drawing. """
     expected = '\n'.join(["        ┌────────┐               ",
                           "q_0: |0>┤        ├───────────────",
                           "        │ Rzz(0) │┌─────────────┐",
                           "q_1: |0>┤        ├┤             ├",
                           "        └────────┘│ Rzz(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)
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
0
 def test_text_cu1_reversebits(self):
     """ cu1 drawing with reversebits"""
     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, reversebits=True)),
                      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)
Ejemplo n.º 20
0
 def test_text_unitary_2q(self):
     """Test unitary matrix gate on 1 qubit"""
     qr = QuantumRegister(3, 'q')
     circuit = QuantumCircuit(qr)
     circuit.unitary(
         [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], qr[1],
         qr[2])
     actual = str(_text_circuit_drawer(circuit))
     expected = '\n'.join([
         '                   ', 'q_0: |0>───────────',
         '        ┌─────────┐', 'q_1: |0>┤         ├',
         '        │ Unitary │', 'q_2: |0>┤         ├', '        └─────────┘'
     ])
     self.assertEqual(actual, expected)
Ejemplo n.º 21
0
    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)
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
0
    def test_text_cu3_reversebits(self):
        """ cu3 drawing with reversebits"""
        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, reversebits=True)), expected)
Ejemplo n.º 24
0
    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)
Ejemplo n.º 25
0
    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)
Ejemplo n.º 26
0
    def test_text_cswap_reversebits(self):
        """ CSwap drawing with reversebits. """
        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, reversebits=True)),
                         expected)
Ejemplo n.º 27
0
    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)
Ejemplo n.º 28
0
    def test_text_justify_left(self):
        """ Drawing with left justify """
        expected = '\n'.join([
            '         ┌───┐   ', 'q1_0: |0>┤ X ├───', '         ├───┤┌─┐',
            'q1_1: |0>┤ H ├┤M├', '         └───┘└╥┘', ' c1_0: 0 ══════╬═',
            '               ║ ', ' c1_1: 0 ══════╩═', '                 '
        ])

        qr1 = QuantumRegister(2, 'q1')
        cr1 = ClassicalRegister(2, 'c1')
        circuit = QuantumCircuit(qr1, cr1)
        circuit.x(qr1[0])
        circuit.h(qr1[1])
        circuit.measure(qr1[1], cr1[1])
        self.assertEqual(str(_text_circuit_drawer(circuit, justify='left')),
                         expected)
    def test_text_measure_2(self):
        """ The measure operator, using some registers. """
        expected = '\n'.join([
            "               ", "q1_0: |0>──────", "               ",
            "q1_1: |0>──────", "            ┌─┐", "q2_0: |0>───┤M├",
            "         ┌─┐└╥┘", "q2_1: |0>┤M├─╫─", "         └╥┘ ║ ",
            " c1_0: 0 ═╬══╬═", "          ║  ║ ", " c1_1: 0 ═╬══╬═",
            "          ║  ║ ", " c2_0: 0 ═╬══╩═", "          ║    ",
            " c2_1: 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)), expected)
Ejemplo n.º 30
0
    def test_text_no_barriers(self):
        """ Drawing without plotbarriers. """
        expected = '\n'.join([
            '         ┌───┐     ', 'q1_0: |0>┤ H ├─────',
            '         ├───┤     ', 'q1_1: |0>┤ H ├─────',
            '         ├───┤     ', 'q2_0: |0>┤ H ├─────',
            '         └───┘┌───┐', 'q2_1: |0>─────┤ H ├', '              └───┘'
        ])

        qr1 = QuantumRegister(2, 'q1')
        qr2 = QuantumRegister(2, 'q2')
        circuit = QuantumCircuit(qr1, qr2)
        circuit.h(qr1)
        circuit.barrier(qr1)
        circuit.barrier(qr2[1])
        circuit.h(qr2)
        self.assertEqual(
            str(_text_circuit_drawer(circuit, plotbarriers=False)), expected)