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 c5e77dac72a84cb3a63aed4f573f6ed5 0 a86142f7e69f41ef99b1a88e8b4a7c21 X c5e77dac72a84cb3a63aed4f573f6ed5--a86142f7e69f41ef99b1a88e8b4a7c21 847c480d6c4442659a7e60933f6342dd 1 a1e7792b9ba0473687e228378e641856 a86142f7e69f41ef99b1a88e8b4a7c21--a1e7792b9ba0473687e228378e641856 1e66068e7f4c495cbea36d29c11d83e7 e70a2377103348d98e426a922f7f8500 Y 847c480d6c4442659a7e60933f6342dd--e70a2377103348d98e426a922f7f8500 e70a2377103348d98e426a922f7f8500--1e66068e7f4c495cbea36d29c11d83e7
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(0))
%3 8fb9f3645dce4cdba3d57d8ce7bd42ce 0 6fc3d042ff8f4d62836b0a6d1812b36f X 8fb9f3645dce4cdba3d57d8ce7bd42ce--6fc3d042ff8f4d62836b0a6d1812b36f 9dcad2d645234e9a802e9f278a9c297f Y 6fc3d042ff8f4d62836b0a6d1812b36f--9dcad2d645234e9a802e9f278a9c297f 433f903fb1cd4abdab765b3638afdd23 9dcad2d645234e9a802e9f278a9c297f--433f903fb1cd4abdab765b3638afdd23
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(1))
%3 66e452f7443645f6874d5bb147822098 0 057ccf3698ed4a44894028440d8760bc X 66e452f7443645f6874d5bb147822098--057ccf3698ed4a44894028440d8760bc 4f1382e97f0245feac73e7cff86405c6 1 89efc1f5e62b48c4b4357d2a513486dc 057ccf3698ed4a44894028440d8760bc--89efc1f5e62b48c4b4357d2a513486dc c0ecedcebf294d8791bfd51dea0d090f 89efc1f5e62b48c4b4357d2a513486dc--c0ecedcebf294d8791bfd51dea0d090f 2dae713c06f04670b8816c5e5e0d8e28 0145042602c346829ebd38c11050adf5 4f1382e97f0245feac73e7cff86405c6--0145042602c346829ebd38c11050adf5 df217cb96b064f239a06aba775297a60 Y 0145042602c346829ebd38c11050adf5--df217cb96b064f239a06aba775297a60 df217cb96b064f239a06aba775297a60--2dae713c06f04670b8816c5e5e0d8e28
from qadence import X, Y, add
from qadence.draw import display

b = add(X(0), Y(1), X(2))
%3 cluster_8b6b9328d3044e728c7537720f7585c6 3d6b91a76f7e4df0b07d58ebb594de83 0 182ceb6c13414eebbee634d99cca91d2 3d6b91a76f7e4df0b07d58ebb594de83--182ceb6c13414eebbee634d99cca91d2 1ca3535f4bf549769592f7d8b59bff2f 1 07f4c94bf90449e5a3b9db1ef3209d36 182ceb6c13414eebbee634d99cca91d2--07f4c94bf90449e5a3b9db1ef3209d36 c90e4c8f56c041e2a60b3eec57890a8e 6ad416042d0e4b1ab2d60c75f598e925 AddBlock 1ca3535f4bf549769592f7d8b59bff2f--6ad416042d0e4b1ab2d60c75f598e925 98ca93562de14fdd99bae8c5738f6bb7 2 6ad416042d0e4b1ab2d60c75f598e925--c90e4c8f56c041e2a60b3eec57890a8e eeac8fd2a89d4c26a1413bb301c4763d 449fab9cc13e4d7b87e1a19342e4a7c0 98ca93562de14fdd99bae8c5738f6bb7--449fab9cc13e4d7b87e1a19342e4a7c0 449fab9cc13e4d7b87e1a19342e4a7c0--eeac8fd2a89d4c26a1413bb301c4763d
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_1bc812e3868d4b658a069a520d498274 cluster_6467f7c8b06c4e29a11ce9995eb88134 rx 6129ad3985fa4bf39dcb9dcb3a12dc8f 0 571cd80692fa463180aa5ad61dc65ed6 X 6129ad3985fa4bf39dcb9dcb3a12dc8f--571cd80692fa463180aa5ad61dc65ed6 628f8ad6c1ec45e2af85dbd49b69c703 1 8759a31e41384fb282765d94ba222e70 571cd80692fa463180aa5ad61dc65ed6--8759a31e41384fb282765d94ba222e70 1614b04f45ba4b929a153a704bf89f16 8759a31e41384fb282765d94ba222e70--1614b04f45ba4b929a153a704bf89f16 694a145ee1ed4e98a5ecc9d649917061 1614b04f45ba4b929a153a704bf89f16--694a145ee1ed4e98a5ecc9d649917061 e6300e7ae748455e8acebf0a7faf2a07 00143a376fd243c9b8bc0cdda5b2e9c8 Y 628f8ad6c1ec45e2af85dbd49b69c703--00143a376fd243c9b8bc0cdda5b2e9c8 1b4ae184d3e745b283a4fcc1ca940a7b 2 f0dc167e1c534135aecaa4abd25b3b24 00143a376fd243c9b8bc0cdda5b2e9c8--f0dc167e1c534135aecaa4abd25b3b24 cdc3482ead09407ca67cf0fd2bdaf02a HamEvo f0dc167e1c534135aecaa4abd25b3b24--cdc3482ead09407ca67cf0fd2bdaf02a cdc3482ead09407ca67cf0fd2bdaf02a--e6300e7ae748455e8acebf0a7faf2a07 4b4a520667e24d6fbda6365f24a2985f b573a980831a481389bb62feee9960a5 RX(x) 1b4ae184d3e745b283a4fcc1ca940a7b--b573a980831a481389bb62feee9960a5 8aecec32b0274f7391a85a26f926f50f 3 bd1213bcf2694a43bc0970c21e609aac b573a980831a481389bb62feee9960a5--bd1213bcf2694a43bc0970c21e609aac bd34967c94f34f66aec0c1f4e20cefd6 t = 10 bd1213bcf2694a43bc0970c21e609aac--bd34967c94f34f66aec0c1f4e20cefd6 bd34967c94f34f66aec0c1f4e20cefd6--4b4a520667e24d6fbda6365f24a2985f c3de2638bc0b48fb9f3524c85872b7d0 b477413aa8404dd0a3ff23b71196979c RX(0.5) 8aecec32b0274f7391a85a26f926f50f--b477413aa8404dd0a3ff23b71196979c d3a6652800a24fcb8eb727a9dc4ad34a X b477413aa8404dd0a3ff23b71196979c--d3a6652800a24fcb8eb727a9dc4ad34a d3a6652800a24fcb8eb727a9dc4ad34a--bd1213bcf2694a43bc0970c21e609aac 02b5e630fbad45f79613fcad74b8f062 d3a6652800a24fcb8eb727a9dc4ad34a--02b5e630fbad45f79613fcad74b8f062 02b5e630fbad45f79613fcad74b8f062--c3de2638bc0b48fb9f3524c85872b7d0
from qadence import feature_map, hea, chain

block = chain(feature_map(4, reupload_scaling="Tower"), hea(4,2))
%3 cluster_82c60f41fe8545dc9d823359a4408bbf HEA cluster_2d9d6d8958e048ac8f723d200aaa4804 Tower Fourier FM 351a97180a214bbaacee7b739696f746 0 7d4dbda40eda4a3d9d02a5152cdec5c4 RX(1.0*phi) 351a97180a214bbaacee7b739696f746--7d4dbda40eda4a3d9d02a5152cdec5c4 61d1eac7a75e4d6da0e48171e3443eab 1 048fbdfd92f641c988bbb4fab6695208 RX(theta₀) 7d4dbda40eda4a3d9d02a5152cdec5c4--048fbdfd92f641c988bbb4fab6695208 bf97bebaf674437fbd1f5009a7b1c04b RY(theta₄) 048fbdfd92f641c988bbb4fab6695208--bf97bebaf674437fbd1f5009a7b1c04b 4380219ac3b24603b30c6c7da1b8aa83 RX(theta₈) bf97bebaf674437fbd1f5009a7b1c04b--4380219ac3b24603b30c6c7da1b8aa83 279f1d3e7b784d709d21ffd25c75faec 4380219ac3b24603b30c6c7da1b8aa83--279f1d3e7b784d709d21ffd25c75faec cc89a46e883641e7ad477e1f644766f2 279f1d3e7b784d709d21ffd25c75faec--cc89a46e883641e7ad477e1f644766f2 9994414243ba46279170f7f266d256ba RX(theta₁₂) cc89a46e883641e7ad477e1f644766f2--9994414243ba46279170f7f266d256ba 7e795a9291924cc7a235614d093b4070 RY(theta₁₆) 9994414243ba46279170f7f266d256ba--7e795a9291924cc7a235614d093b4070 a389153940d64c88bd019670c694d175 RX(theta₂₀) 7e795a9291924cc7a235614d093b4070--a389153940d64c88bd019670c694d175 b3b28434b0a34c22a7a7a9fa0e91c553 a389153940d64c88bd019670c694d175--b3b28434b0a34c22a7a7a9fa0e91c553 941e0a7778154b6fb14295e1891a6a0b b3b28434b0a34c22a7a7a9fa0e91c553--941e0a7778154b6fb14295e1891a6a0b 2d8fd84f39494969ad960c4b680135f9 941e0a7778154b6fb14295e1891a6a0b--2d8fd84f39494969ad960c4b680135f9 ffe6412c52764eac96cda3317e056fd9 46c5c04dd6234007a840f757f1071bfb RX(2.0*phi) 61d1eac7a75e4d6da0e48171e3443eab--46c5c04dd6234007a840f757f1071bfb e75e3bcea6174302bde811ed18e4483f 2 1f666e0c29a94aca9e9714f495d1fdc7 RX(theta₁) 46c5c04dd6234007a840f757f1071bfb--1f666e0c29a94aca9e9714f495d1fdc7 81743a32d75d427b8868be002154fdb4 RY(theta₅) 1f666e0c29a94aca9e9714f495d1fdc7--81743a32d75d427b8868be002154fdb4 e00005a0a08b4e078f1a5d58a2e2aced RX(theta₉) 81743a32d75d427b8868be002154fdb4--e00005a0a08b4e078f1a5d58a2e2aced 745a99a0239e4aa38184df310e9ce9cf X e00005a0a08b4e078f1a5d58a2e2aced--745a99a0239e4aa38184df310e9ce9cf 745a99a0239e4aa38184df310e9ce9cf--279f1d3e7b784d709d21ffd25c75faec ce3eb9faebc042eea059a0d09a0d2419 745a99a0239e4aa38184df310e9ce9cf--ce3eb9faebc042eea059a0d09a0d2419 6b8579ae8d9d4311a62eb9ae339af136 RX(theta₁₃) ce3eb9faebc042eea059a0d09a0d2419--6b8579ae8d9d4311a62eb9ae339af136 aa6ad49f8df84680be32500c2bb9f7f4 RY(theta₁₇) 6b8579ae8d9d4311a62eb9ae339af136--aa6ad49f8df84680be32500c2bb9f7f4 0df683b9bc0146ffa97e535a52fee4a9 RX(theta₂₁) aa6ad49f8df84680be32500c2bb9f7f4--0df683b9bc0146ffa97e535a52fee4a9 12583ccab03d4de6bb64a5192d84d828 X 0df683b9bc0146ffa97e535a52fee4a9--12583ccab03d4de6bb64a5192d84d828 12583ccab03d4de6bb64a5192d84d828--b3b28434b0a34c22a7a7a9fa0e91c553 2d8a07cdd89843b4bcd5ad1a1ba4ecd6 12583ccab03d4de6bb64a5192d84d828--2d8a07cdd89843b4bcd5ad1a1ba4ecd6 2d8a07cdd89843b4bcd5ad1a1ba4ecd6--ffe6412c52764eac96cda3317e056fd9 1f163426e064480aa4cedd5a739ad496 6bedc82b19da415fa414d2602589c0a1 RX(3.0*phi) e75e3bcea6174302bde811ed18e4483f--6bedc82b19da415fa414d2602589c0a1 d01bbb4350a541899a7a293787150895 3 52c4e07bc70743acbf091f622c39f2cc RX(theta₂) 6bedc82b19da415fa414d2602589c0a1--52c4e07bc70743acbf091f622c39f2cc 147ce274ee2d44249740bfc60011f61e RY(theta₆) 52c4e07bc70743acbf091f622c39f2cc--147ce274ee2d44249740bfc60011f61e 6c622f3ae818406cbb2e7c2de424be77 RX(theta₁₀) 147ce274ee2d44249740bfc60011f61e--6c622f3ae818406cbb2e7c2de424be77 742a1cde3093410d8a0ec0fca361e5e8 6c622f3ae818406cbb2e7c2de424be77--742a1cde3093410d8a0ec0fca361e5e8 79d11fb076f24e60b006b1c2b5631466 X 742a1cde3093410d8a0ec0fca361e5e8--79d11fb076f24e60b006b1c2b5631466 79d11fb076f24e60b006b1c2b5631466--ce3eb9faebc042eea059a0d09a0d2419 cc2c5c9ade6442f981d203d480d51085 RX(theta₁₄) 79d11fb076f24e60b006b1c2b5631466--cc2c5c9ade6442f981d203d480d51085 7211e2328a0a45d39a9f7d3ec2ea1f5d RY(theta₁₈) cc2c5c9ade6442f981d203d480d51085--7211e2328a0a45d39a9f7d3ec2ea1f5d 5c8fd54edbde40d995cc29f1916ab59f RX(theta₂₂) 7211e2328a0a45d39a9f7d3ec2ea1f5d--5c8fd54edbde40d995cc29f1916ab59f 4173e5ff7f6e407d8e97285ac543a2b7 5c8fd54edbde40d995cc29f1916ab59f--4173e5ff7f6e407d8e97285ac543a2b7 84d0c2de8d314cad8acbcc4f5c08c5d9 X 4173e5ff7f6e407d8e97285ac543a2b7--84d0c2de8d314cad8acbcc4f5c08c5d9 84d0c2de8d314cad8acbcc4f5c08c5d9--2d8a07cdd89843b4bcd5ad1a1ba4ecd6 84d0c2de8d314cad8acbcc4f5c08c5d9--1f163426e064480aa4cedd5a739ad496 80721a0068d64505a636c00422345ec3 de0779b71fe14a3d84ead8895276a4ab RX(4.0*phi) d01bbb4350a541899a7a293787150895--de0779b71fe14a3d84ead8895276a4ab 14d462c7893a4c57bf7ed310376f78ec RX(theta₃) de0779b71fe14a3d84ead8895276a4ab--14d462c7893a4c57bf7ed310376f78ec a4ede345433747fa81680982d1e5678a RY(theta₇) 14d462c7893a4c57bf7ed310376f78ec--a4ede345433747fa81680982d1e5678a a88dda23c60c4f6fb7b750ef38faa62f RX(theta₁₁) a4ede345433747fa81680982d1e5678a--a88dda23c60c4f6fb7b750ef38faa62f d372684c6e0b453c8e58cf0cc38e8bf8 X a88dda23c60c4f6fb7b750ef38faa62f--d372684c6e0b453c8e58cf0cc38e8bf8 d372684c6e0b453c8e58cf0cc38e8bf8--742a1cde3093410d8a0ec0fca361e5e8 8c053cc60cd8465cace4f38e187ce02d d372684c6e0b453c8e58cf0cc38e8bf8--8c053cc60cd8465cace4f38e187ce02d 7c0996d2637e4a808423a3abeff37433 RX(theta₁₅) 8c053cc60cd8465cace4f38e187ce02d--7c0996d2637e4a808423a3abeff37433 c82268d9f287434b82580de1466672cc RY(theta₁₉) 7c0996d2637e4a808423a3abeff37433--c82268d9f287434b82580de1466672cc df23f8251f014d719909d4843e0da9b9 RX(theta₂₃) c82268d9f287434b82580de1466672cc--df23f8251f014d719909d4843e0da9b9 b133b386087344da9a31bb7d9c303621 X df23f8251f014d719909d4843e0da9b9--b133b386087344da9a31bb7d9c303621 b133b386087344da9a31bb7d9c303621--4173e5ff7f6e407d8e97285ac543a2b7 939e8f738b4540eca1c4d30aea479898 b133b386087344da9a31bb7d9c303621--939e8f738b4540eca1c4d30aea479898 939e8f738b4540eca1c4d30aea479898--80721a0068d64505a636c00422345ec3

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