Skip to content

qadence.draw example plots

Mostly for quick, manual checking of correct plotting output.

from qadence import X, Y, kron
from qadence.draw import display

b = kron(X(0), Y(1))
%3 b05385e2e0534ae8857d7cc39377d7aa 0 ec40ff3edaff49eb815a2d1604ada91c X b05385e2e0534ae8857d7cc39377d7aa--ec40ff3edaff49eb815a2d1604ada91c a769c85792e24ed999110f9f06037f2d 1 ddc2f30b5e524da984a105bd538ecb90 ec40ff3edaff49eb815a2d1604ada91c--ddc2f30b5e524da984a105bd538ecb90 194eb7b3d92048b08953e7007fe82f8a 1e79707a2d3b49ed999880f68db96f3d Y a769c85792e24ed999110f9f06037f2d--1e79707a2d3b49ed999880f68db96f3d 1e79707a2d3b49ed999880f68db96f3d--194eb7b3d92048b08953e7007fe82f8a
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(0))
%3 925178f84de946e99baed64fb76a71f4 0 567fabdd30cb470a8bf3c4a8d32f249f X 925178f84de946e99baed64fb76a71f4--567fabdd30cb470a8bf3c4a8d32f249f b60d12098a7f44568ae9b1478f7251ca Y 567fabdd30cb470a8bf3c4a8d32f249f--b60d12098a7f44568ae9b1478f7251ca 6e3f31aa4c1544998e53112056b90431 b60d12098a7f44568ae9b1478f7251ca--6e3f31aa4c1544998e53112056b90431
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(1))
%3 a1f72007f6604e1cbcaf8f3595430839 0 71910e14a44c48d08e25bd8f3cfc8178 X a1f72007f6604e1cbcaf8f3595430839--71910e14a44c48d08e25bd8f3cfc8178 207cf61a95d9464395d68d7de56e045f 1 c84c45fc283a4c3d9bbb745b852d6f07 71910e14a44c48d08e25bd8f3cfc8178--c84c45fc283a4c3d9bbb745b852d6f07 902f5551e9c0414d813c89f55b57cdaa c84c45fc283a4c3d9bbb745b852d6f07--902f5551e9c0414d813c89f55b57cdaa 40bc5d5759394632a905289f76dd83b1 9d1cc08dfe82430798490ac010b67946 207cf61a95d9464395d68d7de56e045f--9d1cc08dfe82430798490ac010b67946 a530f1745b144758afde747c37387b26 Y 9d1cc08dfe82430798490ac010b67946--a530f1745b144758afde747c37387b26 a530f1745b144758afde747c37387b26--40bc5d5759394632a905289f76dd83b1
from qadence import X, Y, add
from qadence.draw import display

b = add(X(0), Y(1), X(2))
%3 cluster_2e069a92b8e544de8b870a4ec5b98895 ccf88256dde54be8b652c6be61b73961 0 3458329416064af0a58240483188b632 ccf88256dde54be8b652c6be61b73961--3458329416064af0a58240483188b632 79e5352615e348249a48fb50ba6c6cae 1 fc5a81e1ba17416b9b45a1e0875168c6 3458329416064af0a58240483188b632--fc5a81e1ba17416b9b45a1e0875168c6 295979b1f4164805ba3d69ddad2c03ea bf1b16b5c63c446fa507ccf6b222da12 AddBlock 79e5352615e348249a48fb50ba6c6cae--bf1b16b5c63c446fa507ccf6b222da12 ad7d848aec164417b5ac95f385dd1b3a 2 bf1b16b5c63c446fa507ccf6b222da12--295979b1f4164805ba3d69ddad2c03ea 758b60441c724acba56a2e988e46f440 723eefdb7a8048ea8dd07e84bddc84d0 ad7d848aec164417b5ac95f385dd1b3a--723eefdb7a8048ea8dd07e84bddc84d0 723eefdb7a8048ea8dd07e84bddc84d0--758b60441c724acba56a2e988e46f440
from qadence import CNOT, RX, HamEvo, X, Y, Z, chain, kron

rx = kron(RX(3,0.5), RX(2, "x"))
rx.tag = "rx"
gen = chain(Z(i) for i in range(4))

# `chain` puts things in sequence
block = chain(
    kron(X(0), Y(1), rx),
    CNOT(2,3),
    HamEvo(gen, 10)
)
%3 cluster_1d00a7122cee453686f135991cf67cb5 cluster_dd01af9b55ac4c4c8f16b75420f9c41d rx ae1c37f8ec4a45a3ac5a45894af7e20c 0 2ccb61253bbd4010b40f58b219fbf8c9 X ae1c37f8ec4a45a3ac5a45894af7e20c--2ccb61253bbd4010b40f58b219fbf8c9 ccf1939d4aed45cf953c1bc8a0167a8b 1 f160a10a29ba447eba05b1fd631b906b 2ccb61253bbd4010b40f58b219fbf8c9--f160a10a29ba447eba05b1fd631b906b d02902e444074c628c6248b4c65ec59f f160a10a29ba447eba05b1fd631b906b--d02902e444074c628c6248b4c65ec59f a6d4bf6169e54b9b961343e8a026c235 d02902e444074c628c6248b4c65ec59f--a6d4bf6169e54b9b961343e8a026c235 529b30cf677f4137b29a9e315b7a2f97 10b0aca8415e4ad68fb31f940f55ed3e Y ccf1939d4aed45cf953c1bc8a0167a8b--10b0aca8415e4ad68fb31f940f55ed3e b836d590cd19437e9f9678c2a61fef25 2 c4d6319de1f14d44a0152b92a3a46ddc 10b0aca8415e4ad68fb31f940f55ed3e--c4d6319de1f14d44a0152b92a3a46ddc 9659328ff6354a80b05d5d64d900aa17 HamEvo c4d6319de1f14d44a0152b92a3a46ddc--9659328ff6354a80b05d5d64d900aa17 9659328ff6354a80b05d5d64d900aa17--529b30cf677f4137b29a9e315b7a2f97 b809bae91cd9413d950feb0d2a62df04 be4db7540c774a02aed3bb4c0c17f552 RX(x) b836d590cd19437e9f9678c2a61fef25--be4db7540c774a02aed3bb4c0c17f552 a631225fe31645108ac6a8348bf84916 3 4632b514edc44bfea9cdf129a4cf99f3 be4db7540c774a02aed3bb4c0c17f552--4632b514edc44bfea9cdf129a4cf99f3 c11f1d4f27284d238e7f72d630ddd3cf t = 10 4632b514edc44bfea9cdf129a4cf99f3--c11f1d4f27284d238e7f72d630ddd3cf c11f1d4f27284d238e7f72d630ddd3cf--b809bae91cd9413d950feb0d2a62df04 faa8d728cc6949439dd609683d818c63 4145a518634a40beb3a66e683a0134bc RX(0.5) a631225fe31645108ac6a8348bf84916--4145a518634a40beb3a66e683a0134bc 98daa82eedec4f9f834a5195d7e0cf8d X 4145a518634a40beb3a66e683a0134bc--98daa82eedec4f9f834a5195d7e0cf8d 98daa82eedec4f9f834a5195d7e0cf8d--4632b514edc44bfea9cdf129a4cf99f3 966f9b06da55476e92c90ee666c9d3ce 98daa82eedec4f9f834a5195d7e0cf8d--966f9b06da55476e92c90ee666c9d3ce 966f9b06da55476e92c90ee666c9d3ce--faa8d728cc6949439dd609683d818c63
from qadence import feature_map, hea, chain

block = chain(feature_map(4, reupload_scaling="Tower"), hea(4,2))
%3 cluster_bc99a8954973429bbba0cf0d3f8a387d HEA cluster_07ef22aaa7a149e6b02acc69c02c57e7 Tower Fourier FM f6d77154b86947339877e8c49fc3bcb1 0 4a21edb0594a4b1184383ab4733ba19e RX(1.0*phi) f6d77154b86947339877e8c49fc3bcb1--4a21edb0594a4b1184383ab4733ba19e f0c8873e5a874cdab3db53a1fc086662 1 9e8b49117c4f4a5e92812599aa4fc44b RX(theta₀) 4a21edb0594a4b1184383ab4733ba19e--9e8b49117c4f4a5e92812599aa4fc44b 2fee5fa91ce74b0bbeb3003fcaf998ad RY(theta₄) 9e8b49117c4f4a5e92812599aa4fc44b--2fee5fa91ce74b0bbeb3003fcaf998ad 44f13178ca894e2a9363b4751b5838ba RX(theta₈) 2fee5fa91ce74b0bbeb3003fcaf998ad--44f13178ca894e2a9363b4751b5838ba 1d51dcf9a75344b3b072d5c2750f012e 44f13178ca894e2a9363b4751b5838ba--1d51dcf9a75344b3b072d5c2750f012e 4e0b7ce394bf4bcd8d5bdfcf92cda64b 1d51dcf9a75344b3b072d5c2750f012e--4e0b7ce394bf4bcd8d5bdfcf92cda64b e540de393ef64e888b8cb1207b9e5460 RX(theta₁₂) 4e0b7ce394bf4bcd8d5bdfcf92cda64b--e540de393ef64e888b8cb1207b9e5460 d705b6ceede64893b4e6e8b78b5eddc3 RY(theta₁₆) e540de393ef64e888b8cb1207b9e5460--d705b6ceede64893b4e6e8b78b5eddc3 ede423a963ce4bbf8d91b9bb2a8d5249 RX(theta₂₀) d705b6ceede64893b4e6e8b78b5eddc3--ede423a963ce4bbf8d91b9bb2a8d5249 40d50368685a4c27b45ce643885dd937 ede423a963ce4bbf8d91b9bb2a8d5249--40d50368685a4c27b45ce643885dd937 3106035596bd4bd8b1aa2fbcbc8ab40b 40d50368685a4c27b45ce643885dd937--3106035596bd4bd8b1aa2fbcbc8ab40b 5795c4929f294320ae624b8940e26fb0 3106035596bd4bd8b1aa2fbcbc8ab40b--5795c4929f294320ae624b8940e26fb0 f92962abf10e40ce9478d5cecba1320d 74d6ecf3bffc4f41aa443bdd300719b2 RX(2.0*phi) f0c8873e5a874cdab3db53a1fc086662--74d6ecf3bffc4f41aa443bdd300719b2 0f28f610248a4e2795262a7ab090cd2f 2 1dbe4981079b43bb8191aee44db63910 RX(theta₁) 74d6ecf3bffc4f41aa443bdd300719b2--1dbe4981079b43bb8191aee44db63910 3ace9ffed6d2416c8cbb54dbba6666a6 RY(theta₅) 1dbe4981079b43bb8191aee44db63910--3ace9ffed6d2416c8cbb54dbba6666a6 0686d4a43f5f4187a989473e8e72cf2c RX(theta₉) 3ace9ffed6d2416c8cbb54dbba6666a6--0686d4a43f5f4187a989473e8e72cf2c 32221863915d4ea587d25ba76a97f0e6 X 0686d4a43f5f4187a989473e8e72cf2c--32221863915d4ea587d25ba76a97f0e6 32221863915d4ea587d25ba76a97f0e6--1d51dcf9a75344b3b072d5c2750f012e 66a8056dbf654117a5176c7c556ad93d 32221863915d4ea587d25ba76a97f0e6--66a8056dbf654117a5176c7c556ad93d 0d21ef17ffb54838bfb16dbb512f9aa3 RX(theta₁₃) 66a8056dbf654117a5176c7c556ad93d--0d21ef17ffb54838bfb16dbb512f9aa3 69857b621336481d83ffce020b7f545d RY(theta₁₇) 0d21ef17ffb54838bfb16dbb512f9aa3--69857b621336481d83ffce020b7f545d 0a3c9e28b6904e6985d937acccb7a53c RX(theta₂₁) 69857b621336481d83ffce020b7f545d--0a3c9e28b6904e6985d937acccb7a53c e2274bc536f947d584124987ea6eea23 X 0a3c9e28b6904e6985d937acccb7a53c--e2274bc536f947d584124987ea6eea23 e2274bc536f947d584124987ea6eea23--40d50368685a4c27b45ce643885dd937 faf80a2cb6be41409f22eb2682c00255 e2274bc536f947d584124987ea6eea23--faf80a2cb6be41409f22eb2682c00255 faf80a2cb6be41409f22eb2682c00255--f92962abf10e40ce9478d5cecba1320d 6f3d9aec531c4ec19401f39a5bc4938c 6b7cc4a722c54c50bf9f25a2730ebfa6 RX(3.0*phi) 0f28f610248a4e2795262a7ab090cd2f--6b7cc4a722c54c50bf9f25a2730ebfa6 36bd9efd9684450c8f2132c18e7b4183 3 f943a2713d7e4bec9b7ba6203dc20a56 RX(theta₂) 6b7cc4a722c54c50bf9f25a2730ebfa6--f943a2713d7e4bec9b7ba6203dc20a56 22eab67f6aa74d90b79826a21ef2c3e7 RY(theta₆) f943a2713d7e4bec9b7ba6203dc20a56--22eab67f6aa74d90b79826a21ef2c3e7 198ea0ab6c614df39d50b607da19a364 RX(theta₁₀) 22eab67f6aa74d90b79826a21ef2c3e7--198ea0ab6c614df39d50b607da19a364 03ec1f5465c74b6ca8ca9c92296f3774 198ea0ab6c614df39d50b607da19a364--03ec1f5465c74b6ca8ca9c92296f3774 87ccff8d170f47739764edd4f5b4a703 X 03ec1f5465c74b6ca8ca9c92296f3774--87ccff8d170f47739764edd4f5b4a703 87ccff8d170f47739764edd4f5b4a703--66a8056dbf654117a5176c7c556ad93d cdb3464bc03a40e59ce3311c44898f48 RX(theta₁₄) 87ccff8d170f47739764edd4f5b4a703--cdb3464bc03a40e59ce3311c44898f48 105bc73fae594e1bbfb98ad1d6204444 RY(theta₁₈) cdb3464bc03a40e59ce3311c44898f48--105bc73fae594e1bbfb98ad1d6204444 a2fe6fe6295d4967930247775fd123fe RX(theta₂₂) 105bc73fae594e1bbfb98ad1d6204444--a2fe6fe6295d4967930247775fd123fe 13b71bfe733b410e958aed357ec2c507 a2fe6fe6295d4967930247775fd123fe--13b71bfe733b410e958aed357ec2c507 588658765bfc4338b4036552112c17d1 X 13b71bfe733b410e958aed357ec2c507--588658765bfc4338b4036552112c17d1 588658765bfc4338b4036552112c17d1--faf80a2cb6be41409f22eb2682c00255 588658765bfc4338b4036552112c17d1--6f3d9aec531c4ec19401f39a5bc4938c 3d4a896274044b67b1967ea29b0f6a27 c9c359ac825b48beab132809c20464a1 RX(4.0*phi) 36bd9efd9684450c8f2132c18e7b4183--c9c359ac825b48beab132809c20464a1 a4808cc972b34fbebaa0a5f45b8ef06b RX(theta₃) c9c359ac825b48beab132809c20464a1--a4808cc972b34fbebaa0a5f45b8ef06b bf7546eb678f4d058d53faee7e6e3aad RY(theta₇) a4808cc972b34fbebaa0a5f45b8ef06b--bf7546eb678f4d058d53faee7e6e3aad 7072117372db498ea1c7349acf472996 RX(theta₁₁) bf7546eb678f4d058d53faee7e6e3aad--7072117372db498ea1c7349acf472996 7759f07b40c84474847392a450b2c534 X 7072117372db498ea1c7349acf472996--7759f07b40c84474847392a450b2c534 7759f07b40c84474847392a450b2c534--03ec1f5465c74b6ca8ca9c92296f3774 f0408396ae6945daa0550ecbe2e027f2 7759f07b40c84474847392a450b2c534--f0408396ae6945daa0550ecbe2e027f2 bd7cef4a448d4e4385323bf3ab8ced01 RX(theta₁₅) f0408396ae6945daa0550ecbe2e027f2--bd7cef4a448d4e4385323bf3ab8ced01 52e26ed2ce894fb6a909f0ab4562113a RY(theta₁₉) bd7cef4a448d4e4385323bf3ab8ced01--52e26ed2ce894fb6a909f0ab4562113a 1b717f875a24458cb5ce5fbf9e98ec62 RX(theta₂₃) 52e26ed2ce894fb6a909f0ab4562113a--1b717f875a24458cb5ce5fbf9e98ec62 e98ff394b9604059974f6605be16a8c6 X 1b717f875a24458cb5ce5fbf9e98ec62--e98ff394b9604059974f6605be16a8c6 e98ff394b9604059974f6605be16a8c6--13b71bfe733b410e958aed357ec2c507 c729cb7222064cd58085727800dff651 e98ff394b9604059974f6605be16a8c6--c729cb7222064cd58085727800dff651 c729cb7222064cd58085727800dff651--3d4a896274044b67b1967ea29b0f6a27

Developer documentation

This section contains examples in pure graphviz that can be used to understand roughly what is done in the actual drawing backend.

import graphviz

font_name = "Sans-Serif"
font_size = "8"

graph_attr = {
    "rankdir": "LR",  # LR = left to right, TB = top to bottom
    "nodesep": "0.1",  # In inches, tells distance between nodes without edges
    "compound": "true",  # Needed to draw properly edges in hamevo when content is hidden
    "splines": "false",  # Needed to draw control gates vertical lines one over the other
}  # These are the default values for graphs

node_attr = {
    "shape": "box",  # 'box' for normal nodes, 'point' for control gates or 'plaintext' for starting nodes (the qubit label).
    "style": "rounded",  # Unfortunately we can't specify the radius of the rounded, at least for this version
    "fontname": font_name,
    "fontsize": font_size,
    "width": "0.1",  # In inches, it doesn't get tinier than the label font.
    "height": "0.1"  # In inches, it doesn't get tinier than the label font.
}  # These are the defaults values that can be overridden at node declaration.

default_cluster_attr = {
    "fontname": font_name,
    "fontsize": font_size,
    "labelloc": "b",  # location of cluster label. b as bottom, t as top
    "style": "rounded"
} # These are the defaults values that can be overridden at sub graph declaration

hamevo_cluster_attr = {
    "label": "HamEvo(t=10)"
}
hamevo_cluster_attr.update(default_cluster_attr)

h = graphviz.Graph(graph_attr=graph_attr, node_attr=node_attr)
h.node("Hello World!")
h

# Define graph
h = graphviz.Graph(node_attr=node_attr, graph_attr=graph_attr)

# Add start and end nodes
for i in range(4):
    h.node(f's{i}', shape="plaintext", label=f'{i}', group=f"{i}")
    h.node(f'e{i}', style='invis', group=f"{i}")

# Add nodes
h.node('X', group="0")
h.node('Y', group="1")

# Add hamevo and its nodes
hamevo = graphviz.Graph(name='cluster_hamevo', graph_attr=hamevo_cluster_attr)
for i in range(4):
    hamevo.node(f'z{i}', shape="box", style="invis", label=f'{i}', group=f"{i}")
h.subgraph(hamevo)

# Add rx gates cluster and its nodes
cluster_attr = {"label": "RX gates"}
cluster_attr.update(default_cluster_attr)
cluster = graphviz.Graph(name="cluster_0", graph_attr=cluster_attr)
cluster.node('RX(x)', group="2")
cluster.node('RX(0.5)', group="3")
h.subgraph(cluster)

h.node('cnot0', label='', shape='point', width='0.1', group='0')
h.node('cnot1', label='X', group='1')
h.node('cnot2', label='', shape='point', width='0.1', group='2')
h.node('cnot3', label='', shape='point', width='0.1', group='3')

# Add edges
h.edge('s0', 'X')
h.edge('X', 'cnot0')
h.edge('cnot0', 'z0', lhead='cluster_hamevo')
h.edge('z0', 'e0', ltail='cluster_hamevo')
h.edge('s1', 'Y')
h.edge('Y', 'cnot1')
h.edge('cnot1', 'z1', lhead='cluster_hamevo')
h.edge('z1', 'e1', ltail='cluster_hamevo')
h.edge('s2', 'RX(x)')
h.edge('RX(x)', 'cnot2')
h.edge('cnot2', 'z2', lhead='cluster_hamevo')
h.edge('z2', 'e2', ltail='cluster_hamevo')
h.edge('s3', 'RX(0.5)')
h.edge('RX(0.5)', 'cnot3')
h.edge('cnot3', 'z3', lhead='cluster_hamevo')
h.edge('z3', 'e3', ltail='cluster_hamevo')
h.edge('cnot1', 'cnot0', constraint='false')  # constraint: false is needed to draw vertical edges
h.edge('cnot1', 'cnot2', constraint='false')  # constraint: false is needed to draw vertical edges
h.edge('cnot1', 'cnot3', constraint='false')  # constraint: false is needed to draw vertical edges
h

Example of cluster of clusters

# Define graph
h = graphviz.Graph(node_attr=node_attr, graph_attr=graph_attr)

# Define start and end nodes
for i in range(4):
    h.node(f's{i}', shape="plaintext", label=f'{i}', group=f"{i}")
    h.node(f'e{i}', style='invis', group=f"{i}")

# Define outer cluster
cluster_attr = {"label": "Outer cluster"}
cluster_attr.update(default_cluster_attr)
outer_cluster = graphviz.Graph(name="cluster_outer", graph_attr=cluster_attr)

# Define inner cluster 1 and its nodes
cluster_attr = {"label": "Inner cluster 1"}
cluster_attr.update(default_cluster_attr)
inner1_cluster = graphviz.Graph(name="cluster_inner1", graph_attr=cluster_attr)
inner1_cluster.node("a0", group="0")
inner1_cluster.node("a1", group="1")
outer_cluster.subgraph(inner1_cluster)

# Define inner cluster 2 and its nodes
cluster_attr = {"label": "Inner cluster 2"}
cluster_attr.update(default_cluster_attr)
inner2_cluster = graphviz.Graph(name="cluster_inner2", graph_attr=cluster_attr)
inner2_cluster.node("a2", group="2")
inner2_cluster.node("a3", group="3")
outer_cluster.subgraph(inner2_cluster)

# This has to be done here, after inner clusters definitions
h.subgraph(outer_cluster)

# Define more nodes
for i in range(4):
    h.node(f"b{i}", group=f"{i}")

for i in range(4):
    h.edge(f's{i}', f'a{i}')
    h.edge(f'a{i}', f'b{i}')
    h.edge(f'b{i}', f'e{i}')

h