Skip to content

Commit

Permalink
Add support for barriers to latex_drawer()
Browse files Browse the repository at this point in the history
This commit adds support for drawing barriers to the latex_drawer(). The
Qcircuit LaTeX package was recently updated with the \barrier command,
which must be installed in your latex distribution for barriers to work.

Fixes #731
  • Loading branch information
mtreinish committed Aug 22, 2018
1 parent 2582fae commit e0b8990
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions qiskit/tools/visualization/_circuit_visualization.py
Expand Up @@ -521,11 +521,16 @@ def latex(self, aliases=None):
output.write("\t \t")
for j in range(self.img_depth + 1):
cell_str = self._latex[i][j]
# floats can cause "Dimension too large" latex error in xymatrix
# this truncates floats to avoid issue.
cell_str = re.sub(r'[-+]?\d*\.\d{2,}|\d{2,}', _truncate_float,
cell_str)
output.write(cell_str)
# Don't truncate offset float if drawing a barrier
if 'barrier' in cell_str:
output.write(cell_str)
else:
# floats can cause "Dimension too large" latex error in
# xymatrix this truncates floats to avoid issue.
cell_str = re.sub(r'[-+]?\d*\.\d{2,}|\d{2,}',
_truncate_float,
cell_str)
output.write(cell_str)
if j != self.img_depth:
output.write(" & ")
else:
Expand Down Expand Up @@ -1221,13 +1226,23 @@ def _build_latex_array(self, aliases=None):

try:
self._latex[pos_1][columns] = "\\meter"
prev_entry = self._latex[pos_1][columns - 1]
if 'barrier' in prev_entry:
self._latex[pos_1][columns - 1] = prev_entry.replace(
'\\barrier{', '\\barrier[-1.15em]{')
self._latex[pos_2][columns] = \
"\\cw \\cwx[-" + str(pos_2 - pos_1) + "]"
except Exception as e:
raise QISKitError('Error during Latex building: %s' %
str(e))
elif op['name'] == "barrier":
pass
qarglist = [self.qubit_list[i] for i in op['qubits']]
if aliases is not None:
qarglist = map(lambda x: aliases[x], qarglist)
start = self.img_regs[(qarglist[0][0],
qarglist[0][1])]
span = len(op['qubits']) - 1
self._latex[start][columns] += " \\barrier{" + str(span) + "}"
else:
assert False, "bad node data"

Expand Down

0 comments on commit e0b8990

Please sign in to comment.