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 bdca249c35e4476888327740d63ea3ba 0 da88dff4affd4785bd5baaae60c04c98 X bdca249c35e4476888327740d63ea3ba--da88dff4affd4785bd5baaae60c04c98 e820de9261c346b3bf7da40d63974dbf 1 a6baa87289be4394a8a2fcfa5e8fa5f0 da88dff4affd4785bd5baaae60c04c98--a6baa87289be4394a8a2fcfa5e8fa5f0 e6f98c3b9f994757ad729ad502041cbc 19c0b34ca7984568ae966956e5aacbe2 Y e820de9261c346b3bf7da40d63974dbf--19c0b34ca7984568ae966956e5aacbe2 19c0b34ca7984568ae966956e5aacbe2--e6f98c3b9f994757ad729ad502041cbc
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(0))
%3 85b9a3b9ebfd46c195e14097f0149d31 0 b1417a5632d94d6a95204cc757120aca X 85b9a3b9ebfd46c195e14097f0149d31--b1417a5632d94d6a95204cc757120aca 61960d3e47e1430ca570519868e3690e Y b1417a5632d94d6a95204cc757120aca--61960d3e47e1430ca570519868e3690e 9ebe762ef96745aca3635a2599ec3f43 61960d3e47e1430ca570519868e3690e--9ebe762ef96745aca3635a2599ec3f43
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(1))
%3 bec2404a3d74418a927f17b1b01e75f2 0 cc5ed58f48c74bc6a0307e261dec770b X bec2404a3d74418a927f17b1b01e75f2--cc5ed58f48c74bc6a0307e261dec770b e142f072ddc34110ad26531afd3818f3 1 bc709a8d953b4ef69e171408d351709b cc5ed58f48c74bc6a0307e261dec770b--bc709a8d953b4ef69e171408d351709b 0ef62d2f6445424f931e9a2848b548f6 bc709a8d953b4ef69e171408d351709b--0ef62d2f6445424f931e9a2848b548f6 483e21ef199146ed94820e3aa9fc530b 75239e0a160e42cf8eb1ac8217488e84 e142f072ddc34110ad26531afd3818f3--75239e0a160e42cf8eb1ac8217488e84 48b0c05cf5604ad6b559f3af1c34d98e Y 75239e0a160e42cf8eb1ac8217488e84--48b0c05cf5604ad6b559f3af1c34d98e 48b0c05cf5604ad6b559f3af1c34d98e--483e21ef199146ed94820e3aa9fc530b
from qadence import X, Y, add
from qadence.draw import display

b = add(X(0), Y(1), X(2))
%3 cluster_459c2f7836ea4c91b415533c8c2b3c56 f59b2cca0b49481e8e6ea835efa4f54f 0 22a24608e0934633832ecf41c57e06a1 f59b2cca0b49481e8e6ea835efa4f54f--22a24608e0934633832ecf41c57e06a1 0eba7b3b986545e3a17945112d4ccd9e 1 b71be4ed289849f29ec50b393a9e095b 22a24608e0934633832ecf41c57e06a1--b71be4ed289849f29ec50b393a9e095b d3a1005eba554ed08ec3fea40eaf0d22 ca1aa8c6f8fc422ea9a7eb78dd65ec1b AddBlock 0eba7b3b986545e3a17945112d4ccd9e--ca1aa8c6f8fc422ea9a7eb78dd65ec1b 76d05c7138da41c5aab19863c53eb215 2 ca1aa8c6f8fc422ea9a7eb78dd65ec1b--d3a1005eba554ed08ec3fea40eaf0d22 f1a36f9d57354ce0af65b5a82831d95b 719a6878807e47dda174e3d71697a4ab 76d05c7138da41c5aab19863c53eb215--719a6878807e47dda174e3d71697a4ab 719a6878807e47dda174e3d71697a4ab--f1a36f9d57354ce0af65b5a82831d95b
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_6e999264b8e744d9abfec284a527f37e cluster_139ac1e0a23e41538e6711d6b7c65889 rx ac8d71c61bea40e7a069ea649d498ecd 0 6af9acbfbf4c4c3ab2a79855eaeffbd5 X ac8d71c61bea40e7a069ea649d498ecd--6af9acbfbf4c4c3ab2a79855eaeffbd5 60489dc607b543ad8a8be4102c8c23f6 1 09a84c17edd743e680d62c609d5f2a5d 6af9acbfbf4c4c3ab2a79855eaeffbd5--09a84c17edd743e680d62c609d5f2a5d a8089eef95ac4735a000ddf767d9a0ae 09a84c17edd743e680d62c609d5f2a5d--a8089eef95ac4735a000ddf767d9a0ae 85aa53c3a50c45bcb3798f62d86d4c72 a8089eef95ac4735a000ddf767d9a0ae--85aa53c3a50c45bcb3798f62d86d4c72 b08f631fcfd540c4a35a4351ef4c83c4 3359b74e11744836a4a809fd3a24a46d Y 60489dc607b543ad8a8be4102c8c23f6--3359b74e11744836a4a809fd3a24a46d 5a9e30b797d14fdcb78d6fa569798149 2 34e6087981784242ae91a8106aeb6702 3359b74e11744836a4a809fd3a24a46d--34e6087981784242ae91a8106aeb6702 3abe308b11bc4ba4a34cddd495f56fdc HamEvo 34e6087981784242ae91a8106aeb6702--3abe308b11bc4ba4a34cddd495f56fdc 3abe308b11bc4ba4a34cddd495f56fdc--b08f631fcfd540c4a35a4351ef4c83c4 7b5e64e55e0c4696b99ec2fa984bc9d4 a8dac39b3a9445358dc59c2662868da7 RX(x) 5a9e30b797d14fdcb78d6fa569798149--a8dac39b3a9445358dc59c2662868da7 1fe9722ec85149dd81aff1467767e62f 3 08d7869f08de4e5690b9f1c34e158135 a8dac39b3a9445358dc59c2662868da7--08d7869f08de4e5690b9f1c34e158135 f8b10a64d239404c92ea8b5d0a855a05 t = 10 08d7869f08de4e5690b9f1c34e158135--f8b10a64d239404c92ea8b5d0a855a05 f8b10a64d239404c92ea8b5d0a855a05--7b5e64e55e0c4696b99ec2fa984bc9d4 72839a74f38c48f28245b4cb4cd76073 13cdd529e93d462688ca53d65c8ec695 RX(0.5) 1fe9722ec85149dd81aff1467767e62f--13cdd529e93d462688ca53d65c8ec695 936183611691488db0985a81dc27b5d1 X 13cdd529e93d462688ca53d65c8ec695--936183611691488db0985a81dc27b5d1 936183611691488db0985a81dc27b5d1--08d7869f08de4e5690b9f1c34e158135 01e5dad22de5409abb0ba2a88a849633 936183611691488db0985a81dc27b5d1--01e5dad22de5409abb0ba2a88a849633 01e5dad22de5409abb0ba2a88a849633--72839a74f38c48f28245b4cb4cd76073
from qadence import feature_map, hea, chain

block = chain(feature_map(4, reupload_scaling="Tower"), hea(4,2))
%3 cluster_0d64cfc53f7143c79c00ede13ef69372 HEA cluster_2ee57dad57b945a5afdb93f01b596919 Tower Fourier FM dfdcdb0253204ca98c6b3b1c2027bc10 0 cb4c410891c947edb27f8c22d4bea36b RX(1.0*phi) dfdcdb0253204ca98c6b3b1c2027bc10--cb4c410891c947edb27f8c22d4bea36b 0c31d311f0bf4959b1ecf4daaab9fe5e 1 879f332169f1478b8403cdf6c0198118 RX(theta₀) cb4c410891c947edb27f8c22d4bea36b--879f332169f1478b8403cdf6c0198118 6dcc6926db6c4a99bd265d6414d8ce52 RY(theta₄) 879f332169f1478b8403cdf6c0198118--6dcc6926db6c4a99bd265d6414d8ce52 ac211d40cd1344218b718db28ed07b1f RX(theta₈) 6dcc6926db6c4a99bd265d6414d8ce52--ac211d40cd1344218b718db28ed07b1f 7e6377ef4ca2488b8eb6bc4182d6eaeb ac211d40cd1344218b718db28ed07b1f--7e6377ef4ca2488b8eb6bc4182d6eaeb e15f812ec35144f1a27bac488d91a3e2 7e6377ef4ca2488b8eb6bc4182d6eaeb--e15f812ec35144f1a27bac488d91a3e2 23b3c53509794f1c83415effcdb80f92 RX(theta₁₂) e15f812ec35144f1a27bac488d91a3e2--23b3c53509794f1c83415effcdb80f92 181e7286e43b4553bdc1cda45796b8a3 RY(theta₁₆) 23b3c53509794f1c83415effcdb80f92--181e7286e43b4553bdc1cda45796b8a3 d7120e0781cb435799c22c5d2c3298f3 RX(theta₂₀) 181e7286e43b4553bdc1cda45796b8a3--d7120e0781cb435799c22c5d2c3298f3 56a69713a9d84823aff0186f697b08a2 d7120e0781cb435799c22c5d2c3298f3--56a69713a9d84823aff0186f697b08a2 f78df2674d714c47abee0fd47a0c7b12 56a69713a9d84823aff0186f697b08a2--f78df2674d714c47abee0fd47a0c7b12 df2ec8256ef241a68cb2ff7aaf7de61e f78df2674d714c47abee0fd47a0c7b12--df2ec8256ef241a68cb2ff7aaf7de61e 08421159a3a145d09b83145092b26305 4e853bd996ff4783b70a3556ef7d1849 RX(2.0*phi) 0c31d311f0bf4959b1ecf4daaab9fe5e--4e853bd996ff4783b70a3556ef7d1849 cd95ba86c31d4cfab24a40f0b83464ee 2 de046c2441c84194a72d711549c8e76a RX(theta₁) 4e853bd996ff4783b70a3556ef7d1849--de046c2441c84194a72d711549c8e76a 10f7904dea904074afd9f905ceefb6e8 RY(theta₅) de046c2441c84194a72d711549c8e76a--10f7904dea904074afd9f905ceefb6e8 d2df24c4d5a4426ebb6bdb44cac4bd7d RX(theta₉) 10f7904dea904074afd9f905ceefb6e8--d2df24c4d5a4426ebb6bdb44cac4bd7d 7f5f64bda01349ea9e131d41c42ddaba X d2df24c4d5a4426ebb6bdb44cac4bd7d--7f5f64bda01349ea9e131d41c42ddaba 7f5f64bda01349ea9e131d41c42ddaba--7e6377ef4ca2488b8eb6bc4182d6eaeb d0950d63b6f74f0e8294684f7e621224 7f5f64bda01349ea9e131d41c42ddaba--d0950d63b6f74f0e8294684f7e621224 984ccfe9c2c548ba866c44734d845a9f RX(theta₁₃) d0950d63b6f74f0e8294684f7e621224--984ccfe9c2c548ba866c44734d845a9f 0338c58f5f7c477cbd57defee55f7526 RY(theta₁₇) 984ccfe9c2c548ba866c44734d845a9f--0338c58f5f7c477cbd57defee55f7526 923932bcdaa54bcb8f925ab0175f4eaa RX(theta₂₁) 0338c58f5f7c477cbd57defee55f7526--923932bcdaa54bcb8f925ab0175f4eaa 09146a759e0a4e8aafa889bb2233d1ad X 923932bcdaa54bcb8f925ab0175f4eaa--09146a759e0a4e8aafa889bb2233d1ad 09146a759e0a4e8aafa889bb2233d1ad--56a69713a9d84823aff0186f697b08a2 18de6557898443b39d2e2e7128823bef 09146a759e0a4e8aafa889bb2233d1ad--18de6557898443b39d2e2e7128823bef 18de6557898443b39d2e2e7128823bef--08421159a3a145d09b83145092b26305 7b1ea5d883cd4b65aa86663bcc8da7eb 5dabc290270f49f28e6286ea58446a99 RX(3.0*phi) cd95ba86c31d4cfab24a40f0b83464ee--5dabc290270f49f28e6286ea58446a99 87128e59278a43a2b6c9bbec8309a75f 3 519f9f3d472a4b0c98ef3b6d60b9fb9f RX(theta₂) 5dabc290270f49f28e6286ea58446a99--519f9f3d472a4b0c98ef3b6d60b9fb9f 8211359f66404c048fdb7c355576c5c7 RY(theta₆) 519f9f3d472a4b0c98ef3b6d60b9fb9f--8211359f66404c048fdb7c355576c5c7 459c6fe867e94839b810cd4a75b8f29f RX(theta₁₀) 8211359f66404c048fdb7c355576c5c7--459c6fe867e94839b810cd4a75b8f29f c840a5e81c82413195834ac4b7fe9a27 459c6fe867e94839b810cd4a75b8f29f--c840a5e81c82413195834ac4b7fe9a27 71dae0f080ee4bc08dddb56e4638c8d4 X c840a5e81c82413195834ac4b7fe9a27--71dae0f080ee4bc08dddb56e4638c8d4 71dae0f080ee4bc08dddb56e4638c8d4--d0950d63b6f74f0e8294684f7e621224 870a976326714f4e8c50460b25c20f21 RX(theta₁₄) 71dae0f080ee4bc08dddb56e4638c8d4--870a976326714f4e8c50460b25c20f21 028b8feb924c4fc3b19924e2b55c33db RY(theta₁₈) 870a976326714f4e8c50460b25c20f21--028b8feb924c4fc3b19924e2b55c33db a5785eae653f46d4b5de7371e30a9336 RX(theta₂₂) 028b8feb924c4fc3b19924e2b55c33db--a5785eae653f46d4b5de7371e30a9336 458cc8c9f46e46ff9b1b975cd2372efe a5785eae653f46d4b5de7371e30a9336--458cc8c9f46e46ff9b1b975cd2372efe a5cc3dd670694e0abddef16ea5725045 X 458cc8c9f46e46ff9b1b975cd2372efe--a5cc3dd670694e0abddef16ea5725045 a5cc3dd670694e0abddef16ea5725045--18de6557898443b39d2e2e7128823bef a5cc3dd670694e0abddef16ea5725045--7b1ea5d883cd4b65aa86663bcc8da7eb be8799dc99634658a40841831a257c3d bdb9ac23bb814a879de27325db73ae5a RX(4.0*phi) 87128e59278a43a2b6c9bbec8309a75f--bdb9ac23bb814a879de27325db73ae5a 80bacf3cb60d47fd8f448965b1f9fe53 RX(theta₃) bdb9ac23bb814a879de27325db73ae5a--80bacf3cb60d47fd8f448965b1f9fe53 cd642ad80e2d4261a203dcda33dbb625 RY(theta₇) 80bacf3cb60d47fd8f448965b1f9fe53--cd642ad80e2d4261a203dcda33dbb625 2114658f7d094a5cbab25ffb8710a36c RX(theta₁₁) cd642ad80e2d4261a203dcda33dbb625--2114658f7d094a5cbab25ffb8710a36c 588a21d045db45c3bb49b19de767ab5f X 2114658f7d094a5cbab25ffb8710a36c--588a21d045db45c3bb49b19de767ab5f 588a21d045db45c3bb49b19de767ab5f--c840a5e81c82413195834ac4b7fe9a27 d5dd6db0a4e847e1b92f232d14e095a4 588a21d045db45c3bb49b19de767ab5f--d5dd6db0a4e847e1b92f232d14e095a4 b464452b2e364ac0a41a1bd0be65a38d RX(theta₁₅) d5dd6db0a4e847e1b92f232d14e095a4--b464452b2e364ac0a41a1bd0be65a38d 78284ea4aadc453aac427382ecea05d0 RY(theta₁₉) b464452b2e364ac0a41a1bd0be65a38d--78284ea4aadc453aac427382ecea05d0 e8693d80a5364a629a8215ce4cd97553 RX(theta₂₃) 78284ea4aadc453aac427382ecea05d0--e8693d80a5364a629a8215ce4cd97553 344bd04d97e9467cbcd99b06623b8d70 X e8693d80a5364a629a8215ce4cd97553--344bd04d97e9467cbcd99b06623b8d70 344bd04d97e9467cbcd99b06623b8d70--458cc8c9f46e46ff9b1b975cd2372efe 3dc7c11f144447f58330a4c4dbdc48f7 344bd04d97e9467cbcd99b06623b8d70--3dc7c11f144447f58330a4c4dbdc48f7 3dc7c11f144447f58330a4c4dbdc48f7--be8799dc99634658a40841831a257c3d

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