Skip to content
Merged
35 changes: 24 additions & 11 deletions projectq/backends/_circuits/_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Contains a compiler engine which generates TikZ Latex code describing the
circuit.
Expand Down Expand Up @@ -42,9 +41,9 @@ def __init__(self, gate, lines, ctrl_lines):
self.id = -1

def __eq__(self, other):
return (self.gate == other.gate and self.lines == other.lines and
self.ctrl_lines == other.ctrl_lines and
self.id == other.id)
return (self.gate == other.gate and self.lines == other.lines
and self.ctrl_lines == other.ctrl_lines
and self.id == other.id)

def __ne__(self, other):
return not self.__eq__(other)
Expand Down Expand Up @@ -153,6 +152,9 @@ def __init__(self, accept_input=False, default_measure=0):
self._free_lines = []
self._map = dict()

# Order in which qubit lines are drawn
self._drawing_order = []

def is_available(self, cmd):
"""
Specialized implementation of is_available: Returns True if the
Expand Down Expand Up @@ -190,7 +192,7 @@ def set_qubit_locations(self, id_to_loc):
raise RuntimeError("set_qubit_locations() has to be called before"
" applying gates!")

for k in range(min(id_to_loc), max(id_to_loc)+1):
for k in range(min(id_to_loc), max(id_to_loc) + 1):
if k not in id_to_loc:
raise RuntimeError("set_qubit_locations(): Invalid id_to_loc "
"mapping provided. All ids in the provided"
Expand Down Expand Up @@ -221,7 +223,7 @@ def _print_cmd(self, cmd):
self._free_lines.append(qubit_id)

if self.is_last_engine and cmd.gate == Measure:
assert(get_control_count(cmd) == 0)
assert (get_control_count(cmd) == 0)
for qureg in cmd.qubits:
for qubit in qureg:
if self._accept_input:
Expand All @@ -244,7 +246,9 @@ def _print_cmd(self, cmd):
for l in all_lines:
self._qubit_lines[l].append(item)

def get_latex(self):
self._drawing_order.append(all_lines[0])

def get_latex(self, ordered=False, draw_gates_in_parallel=True):
"""
Return the latex document string representing the circuit.

Expand All @@ -256,6 +260,12 @@ def get_latex(self):
python3 my_circuit.py | pdflatex

where my_circuit.py calls this function and prints it to the terminal.

Args:
ordered(bool): flag if the gates should be drawn in the order they
were added to the circuit
draw_gates_in_parallel(bool): flag if parallel gates should be drawn
parallel (True), or not (False)
"""
qubit_lines = dict()

Expand All @@ -271,10 +281,13 @@ def get_latex(self):
new_cmd.id = cmd.lines[0]
qubit_lines[new_line].append(new_cmd)

circuit = []
for lines in qubit_lines:
circuit.append(qubit_lines[lines])
return to_latex(qubit_lines)
drawing_order = None
if ordered:
drawing_order = self._drawing_order

return to_latex(qubit_lines,
drawing_order=drawing_order,
draw_gates_in_parallel=draw_gates_in_parallel)

def receive(self, command_list):
"""
Expand Down
15 changes: 6 additions & 9 deletions projectq/backends/_circuits/_drawer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Tests for projectq.backends.circuits._drawer.py.
"""
Expand All @@ -20,19 +19,17 @@

from projectq import MainEngine
from projectq.cengines import LastEngineException
from projectq.ops import (H,
X,
CNOT,
Measure)
from projectq.ops import (H, X, CNOT, Measure)
from projectq.meta import Control

import projectq.backends._circuits._drawer as _drawer
from projectq.backends._circuits._drawer import CircuitItem, CircuitDrawer


def test_drawer_getlatex():
@pytest.mark.parametrize("ordered", [False, True])
def test_drawer_getlatex(ordered):
old_latex = _drawer.to_latex
_drawer.to_latex = lambda x: x
_drawer.to_latex = lambda x, drawing_order, draw_gates_in_parallel: x

drawer = CircuitDrawer()
drawer.set_qubit_locations({0: 1, 1: 0})
Expand All @@ -46,13 +43,13 @@ def test_drawer_getlatex():
X | qureg[0]
CNOT | (qureg[0], qureg[1])

lines = drawer2.get_latex()
lines = drawer2.get_latex(ordered=ordered)
assert len(lines) == 2
assert len(lines[0]) == 4
assert len(lines[1]) == 3

# check if it was sent on correctly:
lines = drawer.get_latex()
lines = drawer.get_latex(ordered=ordered)
assert len(lines) == 2
assert len(lines[0]) == 3
assert len(lines[1]) == 4
Expand Down
Loading