Skip to content

Commit 4bd6e6f

Browse files
alexandrupalerTakishima
authored andcommitted
Allow selection of drawing order in CircuitDrawer (solves #333) (#334)
* included the possibility to draw the gates in the order they were added to the circuit * edited param names tests should pass now * solved comments * solved comments * passes tests * Test for unordered and ordered circuit drawing * Reindent files in _circuits * Minor adjustments to _drawer.py * added test_body_with_drawing_order * fixed tests and how draw_gates_in_parallel is interpreted * Fix failing tests with Python 2.7 One test of _to_latex_test.py was failing due to precision issues.
1 parent 44ba35b commit 4bd6e6f

File tree

4 files changed

+391
-138
lines changed

4 files changed

+391
-138
lines changed

projectq/backends/_circuits/_drawer.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
1514
"""
1615
Contains a compiler engine which generates TikZ Latex code describing the
1716
circuit.
@@ -42,9 +41,9 @@ def __init__(self, gate, lines, ctrl_lines):
4241
self.id = -1
4342

4443
def __eq__(self, other):
45-
return (self.gate == other.gate and self.lines == other.lines and
46-
self.ctrl_lines == other.ctrl_lines and
47-
self.id == other.id)
44+
return (self.gate == other.gate and self.lines == other.lines
45+
and self.ctrl_lines == other.ctrl_lines
46+
and self.id == other.id)
4847

4948
def __ne__(self, other):
5049
return not self.__eq__(other)
@@ -153,6 +152,9 @@ def __init__(self, accept_input=False, default_measure=0):
153152
self._free_lines = []
154153
self._map = dict()
155154

155+
# Order in which qubit lines are drawn
156+
self._drawing_order = []
157+
156158
def is_available(self, cmd):
157159
"""
158160
Specialized implementation of is_available: Returns True if the
@@ -190,7 +192,7 @@ def set_qubit_locations(self, id_to_loc):
190192
raise RuntimeError("set_qubit_locations() has to be called before"
191193
" applying gates!")
192194

193-
for k in range(min(id_to_loc), max(id_to_loc)+1):
195+
for k in range(min(id_to_loc), max(id_to_loc) + 1):
194196
if k not in id_to_loc:
195197
raise RuntimeError("set_qubit_locations(): Invalid id_to_loc "
196198
"mapping provided. All ids in the provided"
@@ -221,7 +223,7 @@ def _print_cmd(self, cmd):
221223
self._free_lines.append(qubit_id)
222224

223225
if self.is_last_engine and cmd.gate == Measure:
224-
assert(get_control_count(cmd) == 0)
226+
assert (get_control_count(cmd) == 0)
225227
for qureg in cmd.qubits:
226228
for qubit in qureg:
227229
if self._accept_input:
@@ -244,7 +246,9 @@ def _print_cmd(self, cmd):
244246
for l in all_lines:
245247
self._qubit_lines[l].append(item)
246248

247-
def get_latex(self):
249+
self._drawing_order.append(all_lines[0])
250+
251+
def get_latex(self, ordered=False, draw_gates_in_parallel=True):
248252
"""
249253
Return the latex document string representing the circuit.
250254
@@ -256,6 +260,12 @@ def get_latex(self):
256260
python3 my_circuit.py | pdflatex
257261
258262
where my_circuit.py calls this function and prints it to the terminal.
263+
264+
Args:
265+
ordered(bool): flag if the gates should be drawn in the order they
266+
were added to the circuit
267+
draw_gates_in_parallel(bool): flag if parallel gates should be drawn
268+
parallel (True), or not (False)
259269
"""
260270
qubit_lines = dict()
261271

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

274-
circuit = []
275-
for lines in qubit_lines:
276-
circuit.append(qubit_lines[lines])
277-
return to_latex(qubit_lines)
284+
drawing_order = None
285+
if ordered:
286+
drawing_order = self._drawing_order
287+
288+
return to_latex(qubit_lines,
289+
drawing_order=drawing_order,
290+
draw_gates_in_parallel=draw_gates_in_parallel)
278291

279292
def receive(self, command_list):
280293
"""

projectq/backends/_circuits/_drawer_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
1514
"""
1615
Tests for projectq.backends.circuits._drawer.py.
1716
"""
@@ -20,19 +19,17 @@
2019

2120
from projectq import MainEngine
2221
from projectq.cengines import LastEngineException
23-
from projectq.ops import (H,
24-
X,
25-
CNOT,
26-
Measure)
22+
from projectq.ops import (H, X, CNOT, Measure)
2723
from projectq.meta import Control
2824

2925
import projectq.backends._circuits._drawer as _drawer
3026
from projectq.backends._circuits._drawer import CircuitItem, CircuitDrawer
3127

3228

33-
def test_drawer_getlatex():
29+
@pytest.mark.parametrize("ordered", [False, True])
30+
def test_drawer_getlatex(ordered):
3431
old_latex = _drawer.to_latex
35-
_drawer.to_latex = lambda x: x
32+
_drawer.to_latex = lambda x, drawing_order, draw_gates_in_parallel: x
3633

3734
drawer = CircuitDrawer()
3835
drawer.set_qubit_locations({0: 1, 1: 0})
@@ -46,13 +43,13 @@ def test_drawer_getlatex():
4643
X | qureg[0]
4744
CNOT | (qureg[0], qureg[1])
4845

49-
lines = drawer2.get_latex()
46+
lines = drawer2.get_latex(ordered=ordered)
5047
assert len(lines) == 2
5148
assert len(lines[0]) == 4
5249
assert len(lines[1]) == 3
5350

5451
# check if it was sent on correctly:
55-
lines = drawer.get_latex()
52+
lines = drawer.get_latex(ordered=ordered)
5653
assert len(lines) == 2
5754
assert len(lines[0]) == 3
5855
assert len(lines[1]) == 4

0 commit comments

Comments
 (0)