Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text drawer extended for drawing circuits with single classical bit conditioning #6261

Merged
merged 47 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
33d9b6b
Cbit conditional
TharrmashasthaPV Mar 13, 2021
5fda28f
Merge branch 'master' into fixissue1160
TharrmashasthaPV Mar 13, 2021
873e7c7
Merge branch 'master' of https://github.com/TharrmashasthaPV/qiskit-t…
TharrmashasthaPV Mar 13, 2021
adfb361
lint fixed
TharrmashasthaPV Mar 13, 2021
3c57692
Merge branch 'fixissue1160' of https://github.com/TharrmashasthaPV/qi…
TharrmashasthaPV Mar 13, 2021
26430d2
q
TharrmashasthaPV Mar 19, 2021
b46e0b0
added_tests
TharrmashasthaPV Mar 23, 2021
ccf3c0a
Added tests
TharrmashasthaPV Mar 23, 2021
d4f8b4f
lint fix
TharrmashasthaPV Mar 23, 2021
e39c01b
lint-fix
TharrmashasthaPV Mar 23, 2021
7bf04ca
Merge branch 'master' of https://github.com/Qiskit/qiskit-terra into …
TharrmashasthaPV Mar 26, 2021
69b5b55
added extra test
TharrmashasthaPV Mar 26, 2021
2caa5e2
Merge branch 'master' into fixissue1160
TharrmashasthaPV Mar 29, 2021
54f6f16
Merge branch 'master' of https://github.com/TharrmashasthaPV/qiskit-t…
TharrmashasthaPV Apr 7, 2021
00475e5
Merge branch 'master' of https://github.com/TharrmashasthaPV/qiskit-t…
TharrmashasthaPV Apr 16, 2021
b2d9f42
unwanted comment removed
TharrmashasthaPV Apr 16, 2021
bb55831
refactored for bit.register deprication
TharrmashasthaPV Apr 16, 2021
1fb38e3
Merge branch 'master' of upstream Qiskit into fixissue1160
TharrmashasthaPV Apr 20, 2021
830a6fd
text drawer support for single bit cond
TharrmashasthaPV Apr 20, 2021
4afc944
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into is…
TharrmashasthaPV Apr 21, 2021
5dd7cde
Update branch with main
TharrmashasthaPV May 27, 2021
6dfce44
resolved conflicts
TharrmashasthaPV May 27, 2021
f58f945
Added reno
TharrmashasthaPV May 27, 2021
110b87b
Merge branch 'main' into issue1160text
TharrmashasthaPV May 28, 2021
d64b780
Merge branch 'main' into issue1160text
TharrmashasthaPV May 28, 2021
b94f7e0
Merge branch 'main' into issue1160text
TharrmashasthaPV Jun 8, 2021
314cfab
Update utils.py
TharrmashasthaPV Jun 8, 2021
8cafae9
Merge branch 'main' into issue1160text
TharrmashasthaPV Jun 16, 2021
556f6ad
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into is…
TharrmashasthaPV Jun 24, 2021
7f0fcc3
Merge branch 'main' into issue1160text
TharrmashasthaPV Jun 24, 2021
e91e613
Removing redundant structures
TharrmashasthaPV Jun 24, 2021
ac6c7f7
Merge branch 'issue1160text' of https://github.com/TharrmashasthaPV/q…
TharrmashasthaPV Jun 24, 2021
4e5d403
Merge branch 'main' into issue1160text
TharrmashasthaPV Jun 25, 2021
6d1d3a1
Merge main to the issue1160text branch
TharrmashasthaPV Jun 28, 2021
848313e
lint fix
TharrmashasthaPV Jun 28, 2021
4467885
Merge branch 'main' into issue1160text
TharrmashasthaPV Jun 30, 2021
638ff36
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into is…
TharrmashasthaPV Jul 1, 2021
1feb521
Resolved conflicts and merged main to the branch
TharrmashasthaPV Jul 3, 2021
f87ac2e
blacking and linting
TharrmashasthaPV Jul 3, 2021
1efe39a
Black fix
TharrmashasthaPV Jul 3, 2021
a850169
Merge branch 'main' into issue1160text
taalexander Jul 6, 2021
335592e
Merge branch 'main' into issue1160text
TharrmashasthaPV Jul 7, 2021
54387af
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into is…
TharrmashasthaPV Jul 13, 2021
6108eb8
Corrected tests compression and refactored for #6370
TharrmashasthaPV Jul 13, 2021
c4571bd
Merge branch 'main' into issue1160text
TharrmashasthaPV Jul 13, 2021
a5e844a
Merge branch 'main' into issue1160text
1ucian0 Jul 13, 2021
730fd7b
Merge branch 'main' into issue1160text
mergify[bot] Jul 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions qiskit/dagcircuit/dagcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,20 +600,19 @@ def _map_condition(wire_map, condition, target_cregs):
)
except StopIteration as ex:
raise DAGCircuitError(
"Did not find creg containing " "mapped clbit in conditional."
"Did not find creg containing mapped clbit in conditional."
) from ex
else:
# If cond is on a single Clbit then the candidate_creg is
# the target Clbit to which 'bit' is mapped to.
candidate_creg = wire_map[bit]

if new_creg is None:
new_creg = candidate_creg
elif new_creg != candidate_creg:
# Raise if wire_map maps condition creg on to more than one
# creg in target DAG.
raise DAGCircuitError(
"wire_map maps conditional " "register onto more than one creg."
"wire_map maps conditional register onto more than one creg."
)

if not is_reg:
Expand Down
25 changes: 20 additions & 5 deletions qiskit/visualization/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from shutil import get_terminal_size
import sys

from qiskit.circuit import Clbit
from qiskit.circuit import ControlledGate
from qiskit.circuit import Reset
from qiskit.circuit import Measure
Expand Down Expand Up @@ -1377,12 +1378,26 @@ def set_cl_multibox(self, creg, val, top_connect="┴"):
top_connect (char): The char to connect the box on the top.
"""
if self.cregbundle:
label = "= %s" % val
self.set_clbit(creg[0], BoxOnClWire(label=label, top_connect=top_connect))
if isinstance(creg, Clbit):
bit_reg = self._clbit_locations[creg]["register"]
bit_index = self._clbit_locations[creg]["index"]
label_bool = "= T" if val is True else "= F"
label = "%s_%s %s" % (bit_reg.name, bit_index, label_bool)
self.set_clbit(creg, BoxOnClWire(label=label, top_connect=top_connect))
else:
label = "= %s" % val
self.set_clbit(creg[0], BoxOnClWire(label=label, top_connect=top_connect))
else:
clbit = [bit for bit in self.clbits if self._clbit_locations[bit]["register"] == creg]
cond_bin = bin(val)[2:].zfill(len(clbit))
self.set_cond_bullets(cond_bin, clbits=clbit)
if isinstance(creg, Clbit):
clbit = [creg]
cond_bin = "1" if val is True else "0"
self.set_cond_bullets(cond_bin, clbit)
else:
clbit = [
bit for bit in self.clbits if self._clbit_locations[bit]["register"] == creg
]
cond_bin = bin(val)[2:].zfill(len(clbit))
self.set_cond_bullets(cond_bin, clbits=clbit)

def set_cond_bullets(self, val, clbits):
"""Sets bullets for classical conditioning when cregbundle=False.
Expand Down
8 changes: 7 additions & 1 deletion qiskit/visualization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qiskit.quantum_info.operators.symplectic import PauliTable, SparsePauliOp
from qiskit.visualization.exceptions import VisualizationError
from qiskit.circuit import Measure, ControlledGate, Gate, Instruction, Delay, BooleanExpression
from qiskit.circuit import Clbit
from qiskit.circuit.tools import pi_check
from qiskit.exceptions import MissingOptionalLibraryError

Expand Down Expand Up @@ -298,6 +299,7 @@ def __init__(self, dag, justification, measure_map, reverse_bits):
self.qubits = dag.qubits
self.justification = justification
self.measure_map = measure_map
self.cregs = [self.dag.cregs[reg] for reg in self.dag.cregs]
self.reverse_bits = reverse_bits

if self.justification == "left":
Expand Down Expand Up @@ -347,7 +349,11 @@ def slide_from_left(self, node, index):
last_insertable_index = -1
index_stop = -1
if node.op.condition:
index_stop = self.measure_map[node.op.condition[0]]
if isinstance(node.op.condition[0], Clbit):
cond_reg = [creg for creg in self.cregs if node.op.condition[0] in creg]
index_stop = self.measure_map[cond_reg[0]]
else:
index_stop = self.measure_map[node.op.condition[0]]
elif node.cargs:
for carg in node.cargs:
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
features:
- |
The ``text`` output method for the :func:`qiskit.visualization.circuit_drawer`
function and the :meth:`~qiskit.circuit.QuantumCircuit.draw` method can now
draw circuits that contain gates with single bit condition. This was added for
compatibility of text drawer with the new feature of supporting classical
conditioning of gates on single classical bits.
52 changes: 52 additions & 0 deletions test/python/visualization/test_circuit_text_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,58 @@ def test_text_conditional_measure(self):

self.assertEqual(str(_text_circuit_drawer(circuit)), expected)

def test_text_bit_conditional(self):
"""Test bit conditions on gates"""

qr = QuantumRegister(2, "qr")
cr = ClassicalRegister(2, "cr")
circuit = QuantumCircuit(qr, cr)
circuit.h(qr[0]).c_if(cr[0], 1)
circuit.h(qr[1]).c_if(cr[1], 0)

expected = "\n".join(
[
" ┌───┐ ",
"qr_0: |0>┤ H ├─────",
" └─╥─┘┌───┐",
"qr_1: |0>──╫──┤ H ├",
" ║ └─╥─┘",
" cr_0: 0 ══■════╬══",
" =1 ║ ",
" cr_1: 0 ═══════o══",
" =0 ",
]
)

self.assertEqual(str(_text_circuit_drawer(circuit)), expected)

def test_text_bit_conditional_cregbundle(self):
"""Test bit conditions on gates when cregbundle=True"""

qr = QuantumRegister(2, "qr")
cr = ClassicalRegister(2, "cr")
circuit = QuantumCircuit(qr, cr)
circuit.h(qr[0]).c_if(cr[0], 1)
circuit.h(qr[1]).c_if(cr[1], 0)

expected = "\n".join(
[
" ┌───┐ ",
"qr_0: |0>───┤ H ├────────────────",
" └─╥─┘ ┌───┐ ",
"qr_1: |0>─────╫─────────┤ H ├────",
" ║ └─╥─┘ ",
" ┌────╨─────┐┌────╨─────┐",
" cr: 0 2/╡ cr_0 = T ╞╡ cr_1 = F ╞",
" └──────────┘└──────────┘",
]
)

self.assertEqual(
str(_text_circuit_drawer(circuit, cregbundle=True, vertical_compression="medium")),
expected,
)

def test_text_conditional_reverse_bits_1(self):
"""Classical condition on 2q2c circuit with cregbundle=False and reverse bits"""
qr = QuantumRegister(2, "qr")
Expand Down