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 6a2838f5a1d14d62b814fcd533231a78 0 56c42de851584330bb86b5255d37b2dd X 6a2838f5a1d14d62b814fcd533231a78--56c42de851584330bb86b5255d37b2dd 76a43a32978241ec864aa6b540d99cfb 1 0fa22c5c606042e8acdc9c7eb0d1cced 56c42de851584330bb86b5255d37b2dd--0fa22c5c606042e8acdc9c7eb0d1cced d13386ce324242d282e6a48a854becd0 a458ccafbb9d4e8686538b8d8ea72a4a Y 76a43a32978241ec864aa6b540d99cfb--a458ccafbb9d4e8686538b8d8ea72a4a a458ccafbb9d4e8686538b8d8ea72a4a--d13386ce324242d282e6a48a854becd0
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(0))
%3 bd2cd870144547fc971162f8b6925d1d 0 2add641e109e4cc8b0853e96c8b2a87b X bd2cd870144547fc971162f8b6925d1d--2add641e109e4cc8b0853e96c8b2a87b d87950eaee2e4fa5a5c1eaa8d9b247c8 Y 2add641e109e4cc8b0853e96c8b2a87b--d87950eaee2e4fa5a5c1eaa8d9b247c8 30bfe95a0ebe45f3b58e7a0b9630e5f0 d87950eaee2e4fa5a5c1eaa8d9b247c8--30bfe95a0ebe45f3b58e7a0b9630e5f0
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(1))
%3 54d1deed837b4bed9046e9bd8fa8c662 0 51167a5fc64c41f09837c432c902564b X 54d1deed837b4bed9046e9bd8fa8c662--51167a5fc64c41f09837c432c902564b a716a3af191344dd80c86b22d6cb765a 1 f11b62937fc94a91afa35b89deb40270 51167a5fc64c41f09837c432c902564b--f11b62937fc94a91afa35b89deb40270 6f1e027a9e5942ef8516011e65d2c49b f11b62937fc94a91afa35b89deb40270--6f1e027a9e5942ef8516011e65d2c49b c42ecd2c5c014bd58f872ba5ec07f0d0 9aaf7b0ba2584d3b80bcac106b003236 a716a3af191344dd80c86b22d6cb765a--9aaf7b0ba2584d3b80bcac106b003236 ff0c485519b0475097a0f2bbe169f4b1 Y 9aaf7b0ba2584d3b80bcac106b003236--ff0c485519b0475097a0f2bbe169f4b1 ff0c485519b0475097a0f2bbe169f4b1--c42ecd2c5c014bd58f872ba5ec07f0d0
from qadence import X, Y, add
from qadence.draw import display

b = add(X(0), Y(1), X(2))
%3 cluster_5fc975517f674c5089aa2fb850f6403c d40b203edb344a44b5dc27de77534ece 0 952c980824c4481690c955b359d933b2 d40b203edb344a44b5dc27de77534ece--952c980824c4481690c955b359d933b2 6abc5e3e9e514dc8810b9acbce558312 1 ac63f120e08c4175b0dcf985645a8538 952c980824c4481690c955b359d933b2--ac63f120e08c4175b0dcf985645a8538 7d96f8f4fc0d47418d430d5cf046ac2b 697993a250c745f29bedebd63bfcd70f AddBlock 6abc5e3e9e514dc8810b9acbce558312--697993a250c745f29bedebd63bfcd70f 6f4b24aac49e47ba901735035acd328a 2 697993a250c745f29bedebd63bfcd70f--7d96f8f4fc0d47418d430d5cf046ac2b e45419849e514a44926b8c96795ffd4a f556a892fc744576aab0721ee86f7618 6f4b24aac49e47ba901735035acd328a--f556a892fc744576aab0721ee86f7618 f556a892fc744576aab0721ee86f7618--e45419849e514a44926b8c96795ffd4a
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_9953768a094145d2a3626123c5a84583 cluster_999937c195dd498788b0ce25f333c0e7 rx 8460b2e044d241adb7979c8031b0ba96 0 7efff87dedc1483dbe7aea37ae5107fc X 8460b2e044d241adb7979c8031b0ba96--7efff87dedc1483dbe7aea37ae5107fc 78e6c0c23e7a40f6bacef538e33aedbb 1 6e43e02165324b5eadee72c49760e86a 7efff87dedc1483dbe7aea37ae5107fc--6e43e02165324b5eadee72c49760e86a bd3123cd86444a4ea1b594fe7541cc31 6e43e02165324b5eadee72c49760e86a--bd3123cd86444a4ea1b594fe7541cc31 180c723aa0e4445995a9279b5be27c59 bd3123cd86444a4ea1b594fe7541cc31--180c723aa0e4445995a9279b5be27c59 f9e3c27cd76e4c8581c3148f3b9dbaa2 83966574841547fb83649eb286dfb658 Y 78e6c0c23e7a40f6bacef538e33aedbb--83966574841547fb83649eb286dfb658 f5987254700149118fb32d39a2820c08 2 5a10e31d3be544b4a2fbb0061120d7fc 83966574841547fb83649eb286dfb658--5a10e31d3be544b4a2fbb0061120d7fc 0a71c19b84ec46e4b9d7fd0d3b4d7c59 HamEvo 5a10e31d3be544b4a2fbb0061120d7fc--0a71c19b84ec46e4b9d7fd0d3b4d7c59 0a71c19b84ec46e4b9d7fd0d3b4d7c59--f9e3c27cd76e4c8581c3148f3b9dbaa2 220d91cd8f17493f92345c887cbae078 d07a66ed6d884bb08e507e97ef906f90 RX(x) f5987254700149118fb32d39a2820c08--d07a66ed6d884bb08e507e97ef906f90 eba0c56313fb42d69e5e1f0da48ca5a2 3 7488f5e109e647ce8c4fd0051b5b3ff8 d07a66ed6d884bb08e507e97ef906f90--7488f5e109e647ce8c4fd0051b5b3ff8 c5f6d0eff192425591e9ba8db0ce06f9 t = 10 7488f5e109e647ce8c4fd0051b5b3ff8--c5f6d0eff192425591e9ba8db0ce06f9 c5f6d0eff192425591e9ba8db0ce06f9--220d91cd8f17493f92345c887cbae078 abff094a0daa482593b5ace242bf6ae3 06e97f77111242aaab292348c7e1c135 RX(0.5) eba0c56313fb42d69e5e1f0da48ca5a2--06e97f77111242aaab292348c7e1c135 f73c8ffbef3a494e9ca88171d6e58813 X 06e97f77111242aaab292348c7e1c135--f73c8ffbef3a494e9ca88171d6e58813 f73c8ffbef3a494e9ca88171d6e58813--7488f5e109e647ce8c4fd0051b5b3ff8 c9ca6d40e1514d89b7b8edf96050c05f f73c8ffbef3a494e9ca88171d6e58813--c9ca6d40e1514d89b7b8edf96050c05f c9ca6d40e1514d89b7b8edf96050c05f--abff094a0daa482593b5ace242bf6ae3
from qadence import feature_map, hea, chain

block = chain(feature_map(4, fm_type="tower"), hea(4,2))
%3 cluster_8bb11fe4695c465f87fdc00b79c4c3a5 HEA cluster_5ea0f34746304801b6455766e43951a7 Tower Chebyshev FM 8b6304cf01454238a04fa718e2e7f2d5 0 ac92a94a8abe41a7886b0ff0814fa7ff RX(1.0*acos(phi)) 8b6304cf01454238a04fa718e2e7f2d5--ac92a94a8abe41a7886b0ff0814fa7ff 86a92d4a5ffa46d2868225a78f37894e 1 f69e09141b5d44a5a0b939f97cc095ed RX(theta₀) ac92a94a8abe41a7886b0ff0814fa7ff--f69e09141b5d44a5a0b939f97cc095ed 0ae9cb340be24747833fc728db36bcad RY(theta₄) f69e09141b5d44a5a0b939f97cc095ed--0ae9cb340be24747833fc728db36bcad d589a80404444aa2b5af4ae8c3862252 RX(theta₈) 0ae9cb340be24747833fc728db36bcad--d589a80404444aa2b5af4ae8c3862252 c97f6e22b4d84b59b153f005950bf4af d589a80404444aa2b5af4ae8c3862252--c97f6e22b4d84b59b153f005950bf4af 792123e634ad4c2e9a08ed5e44c2365b c97f6e22b4d84b59b153f005950bf4af--792123e634ad4c2e9a08ed5e44c2365b 7e14aa46c45d41c2a8a8debe9c2a50da RX(theta₁₂) 792123e634ad4c2e9a08ed5e44c2365b--7e14aa46c45d41c2a8a8debe9c2a50da e07ba6ed2f8344d4806ba7e592204537 RY(theta₁₆) 7e14aa46c45d41c2a8a8debe9c2a50da--e07ba6ed2f8344d4806ba7e592204537 1c0175621d694d529d5515f8557166db RX(theta₂₀) e07ba6ed2f8344d4806ba7e592204537--1c0175621d694d529d5515f8557166db 951e692c15024505853d6728a8f1268f 1c0175621d694d529d5515f8557166db--951e692c15024505853d6728a8f1268f 6663119da7484d0d930d3f3121170b04 951e692c15024505853d6728a8f1268f--6663119da7484d0d930d3f3121170b04 4e6e837e15b645f188c9eff3ac37e99e 6663119da7484d0d930d3f3121170b04--4e6e837e15b645f188c9eff3ac37e99e cb102c9a044e4b40847b7f6116d7bc01 b618882b7925466aaa7d0adf863e60f6 RX(2.0*acos(phi)) 86a92d4a5ffa46d2868225a78f37894e--b618882b7925466aaa7d0adf863e60f6 c8e0acc79a5e4406951df7690a05d4cd 2 942df93fb31a4405914394a2c85917c3 RX(theta₁) b618882b7925466aaa7d0adf863e60f6--942df93fb31a4405914394a2c85917c3 92cecb5291724760a59ccf95b77c23cf RY(theta₅) 942df93fb31a4405914394a2c85917c3--92cecb5291724760a59ccf95b77c23cf bf737bc1d68d4e039a5199f54beb5174 RX(theta₉) 92cecb5291724760a59ccf95b77c23cf--bf737bc1d68d4e039a5199f54beb5174 8bf100cfae4a40ce9a0e17ecf98a4eeb X bf737bc1d68d4e039a5199f54beb5174--8bf100cfae4a40ce9a0e17ecf98a4eeb 8bf100cfae4a40ce9a0e17ecf98a4eeb--c97f6e22b4d84b59b153f005950bf4af 2a24cb589ba14c5ab7bf4cc576f8daf7 8bf100cfae4a40ce9a0e17ecf98a4eeb--2a24cb589ba14c5ab7bf4cc576f8daf7 a8a12c71d7ac4051b89f7b58015d5b24 RX(theta₁₃) 2a24cb589ba14c5ab7bf4cc576f8daf7--a8a12c71d7ac4051b89f7b58015d5b24 24421e8620db4ed9a75ee24b1254a5c6 RY(theta₁₇) a8a12c71d7ac4051b89f7b58015d5b24--24421e8620db4ed9a75ee24b1254a5c6 5ccffe7a697d4167a062acc7494d6b4a RX(theta₂₁) 24421e8620db4ed9a75ee24b1254a5c6--5ccffe7a697d4167a062acc7494d6b4a c29d0fea9aff40228f15957706e7bc5b X 5ccffe7a697d4167a062acc7494d6b4a--c29d0fea9aff40228f15957706e7bc5b c29d0fea9aff40228f15957706e7bc5b--951e692c15024505853d6728a8f1268f 258b4459307d4cc18f0b8b32b69ae8e0 c29d0fea9aff40228f15957706e7bc5b--258b4459307d4cc18f0b8b32b69ae8e0 258b4459307d4cc18f0b8b32b69ae8e0--cb102c9a044e4b40847b7f6116d7bc01 fa07c1f784364df9bbaacb8859926fce 0d6662d01a28428094f66a785d2c2df8 RX(3.0*acos(phi)) c8e0acc79a5e4406951df7690a05d4cd--0d6662d01a28428094f66a785d2c2df8 04963578e3bd48d5adcacf88bfe7aea1 3 54e6be4c23b04e4ab9d054734cbfd20c RX(theta₂) 0d6662d01a28428094f66a785d2c2df8--54e6be4c23b04e4ab9d054734cbfd20c 469c3e196cc444b8a356780f8e0e4682 RY(theta₆) 54e6be4c23b04e4ab9d054734cbfd20c--469c3e196cc444b8a356780f8e0e4682 a617a614a6ec4c39ad494593ddc7434e RX(theta₁₀) 469c3e196cc444b8a356780f8e0e4682--a617a614a6ec4c39ad494593ddc7434e fe8d4b0a8dc9449396edb3d651e116bc a617a614a6ec4c39ad494593ddc7434e--fe8d4b0a8dc9449396edb3d651e116bc 13c39304cb314c599b433b3f4d5369d2 X fe8d4b0a8dc9449396edb3d651e116bc--13c39304cb314c599b433b3f4d5369d2 13c39304cb314c599b433b3f4d5369d2--2a24cb589ba14c5ab7bf4cc576f8daf7 0e506479a2154e07b4aba3b28e054108 RX(theta₁₄) 13c39304cb314c599b433b3f4d5369d2--0e506479a2154e07b4aba3b28e054108 339f98dcccaa4b228940ba9cbf50b84c RY(theta₁₈) 0e506479a2154e07b4aba3b28e054108--339f98dcccaa4b228940ba9cbf50b84c f61d0a578cd04ed688ffe731e1b66cc3 RX(theta₂₂) 339f98dcccaa4b228940ba9cbf50b84c--f61d0a578cd04ed688ffe731e1b66cc3 fd46dcb251494d85a30c268fbd09bc4e f61d0a578cd04ed688ffe731e1b66cc3--fd46dcb251494d85a30c268fbd09bc4e b540decfa68048a5b1bf181e30881d24 X fd46dcb251494d85a30c268fbd09bc4e--b540decfa68048a5b1bf181e30881d24 b540decfa68048a5b1bf181e30881d24--258b4459307d4cc18f0b8b32b69ae8e0 b540decfa68048a5b1bf181e30881d24--fa07c1f784364df9bbaacb8859926fce 6792524466b24de2a7dc861fc058710c a85e51438a9d4fe881bc635a4fe67baa RX(4.0*acos(phi)) 04963578e3bd48d5adcacf88bfe7aea1--a85e51438a9d4fe881bc635a4fe67baa 579f98b621fc408ab352ddba1ff654cc RX(theta₃) a85e51438a9d4fe881bc635a4fe67baa--579f98b621fc408ab352ddba1ff654cc ec495ba0c8a645f4ab3e57ec561acd2a RY(theta₇) 579f98b621fc408ab352ddba1ff654cc--ec495ba0c8a645f4ab3e57ec561acd2a 4a2eeffc08ee476aaf65b89b6673438d RX(theta₁₁) ec495ba0c8a645f4ab3e57ec561acd2a--4a2eeffc08ee476aaf65b89b6673438d d32f97fd6d754603b984e719b010c38c X 4a2eeffc08ee476aaf65b89b6673438d--d32f97fd6d754603b984e719b010c38c d32f97fd6d754603b984e719b010c38c--fe8d4b0a8dc9449396edb3d651e116bc aae868abff1d42be8e6abf459d405bc7 d32f97fd6d754603b984e719b010c38c--aae868abff1d42be8e6abf459d405bc7 adcc74e802de4905ae8abe13f9c657aa RX(theta₁₅) aae868abff1d42be8e6abf459d405bc7--adcc74e802de4905ae8abe13f9c657aa c3868cbbc09a4b298ef210199f16ec52 RY(theta₁₉) adcc74e802de4905ae8abe13f9c657aa--c3868cbbc09a4b298ef210199f16ec52 c256a62c488349e29f3e951a90d6d7a3 RX(theta₂₃) c3868cbbc09a4b298ef210199f16ec52--c256a62c488349e29f3e951a90d6d7a3 e4ce326b2cff41a89c3fda4769ddddcd X c256a62c488349e29f3e951a90d6d7a3--e4ce326b2cff41a89c3fda4769ddddcd e4ce326b2cff41a89c3fda4769ddddcd--fd46dcb251494d85a30c268fbd09bc4e b6ffe7216b404efb96b6be9774b4dfce e4ce326b2cff41a89c3fda4769ddddcd--b6ffe7216b404efb96b6be9774b4dfce b6ffe7216b404efb96b6be9774b4dfce--6792524466b24de2a7dc861fc058710c

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