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 109ad1a1bce94013ba33d35d43b6748d 0 d0fc010d03374a42b60a3fcab624df67 X 109ad1a1bce94013ba33d35d43b6748d--d0fc010d03374a42b60a3fcab624df67 fd6701e38d444436be30eb0b569b94ff 1 8d70fc34ccf74933bd24b8aecae9cb45 d0fc010d03374a42b60a3fcab624df67--8d70fc34ccf74933bd24b8aecae9cb45 54a0507f485243fd92f96871321d2bfa a9c7c33e017e4843af71e1489735f6ab Y fd6701e38d444436be30eb0b569b94ff--a9c7c33e017e4843af71e1489735f6ab a9c7c33e017e4843af71e1489735f6ab--54a0507f485243fd92f96871321d2bfa
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(0))
%3 eb11e535b6614bb286ab8956a6f77eb8 0 8437a1700a244814a34a7e9a9bc263ef X eb11e535b6614bb286ab8956a6f77eb8--8437a1700a244814a34a7e9a9bc263ef 04d733a382b84474b0ac095155476552 Y 8437a1700a244814a34a7e9a9bc263ef--04d733a382b84474b0ac095155476552 380d223b97c1430995798acf14626c7f 04d733a382b84474b0ac095155476552--380d223b97c1430995798acf14626c7f
from qadence import X, Y, chain
from qadence.draw import display

b = chain(X(0), Y(1))
%3 0fe8355ead264aa8aad4435b5c10f426 0 cc8e845c079c4366ad7e7e635f6bbe3a X 0fe8355ead264aa8aad4435b5c10f426--cc8e845c079c4366ad7e7e635f6bbe3a 113b47574e3b422f917bfdae17e1ce14 1 04b47de7854946599353edb55d224bd4 cc8e845c079c4366ad7e7e635f6bbe3a--04b47de7854946599353edb55d224bd4 1098207122b640c5b499d8b229f0c8ba 04b47de7854946599353edb55d224bd4--1098207122b640c5b499d8b229f0c8ba 2c524d14d7b24f01a6a023aa0a882ef1 768b1035bb6f49cca0eff8ee48125f66 113b47574e3b422f917bfdae17e1ce14--768b1035bb6f49cca0eff8ee48125f66 4596c897da7945209b3fc620cfd9039f Y 768b1035bb6f49cca0eff8ee48125f66--4596c897da7945209b3fc620cfd9039f 4596c897da7945209b3fc620cfd9039f--2c524d14d7b24f01a6a023aa0a882ef1
from qadence import X, Y, add
from qadence.draw import display

b = add(X(0), Y(1), X(2))
%3 cluster_3e2cf219df1349f4a9223c5745977e3d b0ca98326c1f4376a648b3771aa92738 0 736190a11fd24e5d988b61d06961c011 b0ca98326c1f4376a648b3771aa92738--736190a11fd24e5d988b61d06961c011 d3c3a9ce3ce14dc0a235ca83f2f16171 1 a152301d75c24de08db0e58712d3dee0 736190a11fd24e5d988b61d06961c011--a152301d75c24de08db0e58712d3dee0 6729d81aaf9743a0a2cbe22b82adf6e9 01a37f3493e0441ca8b4d69225ab7533 AddBlock d3c3a9ce3ce14dc0a235ca83f2f16171--01a37f3493e0441ca8b4d69225ab7533 f2173a1301b44467b72b5b8dd7690557 2 01a37f3493e0441ca8b4d69225ab7533--6729d81aaf9743a0a2cbe22b82adf6e9 f9c67049f80f4ff69e91fa4f49c83869 775f1282106b4aed94475424f0c4be71 f2173a1301b44467b72b5b8dd7690557--775f1282106b4aed94475424f0c4be71 775f1282106b4aed94475424f0c4be71--f9c67049f80f4ff69e91fa4f49c83869
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_a3c2157180d94fefb8b17527bef4e8ed cluster_5c5256e65cb4419ea97f5a4198ce0a83 rx 9791abd453e44b68bc8e7d73ed5eac12 0 78fe4ceec8894cefb72b4f5683a88c52 X 9791abd453e44b68bc8e7d73ed5eac12--78fe4ceec8894cefb72b4f5683a88c52 2587ba0a3e6c410a9640109d19fa4a0b 1 08dc8b8c921b468b9b81c1f071fbada9 78fe4ceec8894cefb72b4f5683a88c52--08dc8b8c921b468b9b81c1f071fbada9 c9d4579ef8ea47238979c3cb8ac00ffe 08dc8b8c921b468b9b81c1f071fbada9--c9d4579ef8ea47238979c3cb8ac00ffe 4f6aded2f6094bb9b06a1f7724660015 c9d4579ef8ea47238979c3cb8ac00ffe--4f6aded2f6094bb9b06a1f7724660015 07007ec8a1444c0d96bb4a198bce8deb 2bc2c5df6621400e91ed531ec8247d66 Y 2587ba0a3e6c410a9640109d19fa4a0b--2bc2c5df6621400e91ed531ec8247d66 8a544687da6a494db2237583e6c56780 2 2fecb4a05191481ca8c57a47eedf842c 2bc2c5df6621400e91ed531ec8247d66--2fecb4a05191481ca8c57a47eedf842c 35d2188ce79840139ff4db1c3d12ecc7 HamEvo 2fecb4a05191481ca8c57a47eedf842c--35d2188ce79840139ff4db1c3d12ecc7 35d2188ce79840139ff4db1c3d12ecc7--07007ec8a1444c0d96bb4a198bce8deb 5e0abc0184ac4bc5a27108d82bc20e25 87caddc9c7324d01bd909743b339133c RX(x) 8a544687da6a494db2237583e6c56780--87caddc9c7324d01bd909743b339133c 625ea252d483459f98353b038d78d14b 3 b6ef37b9bc834cebb380fa9de064021b 87caddc9c7324d01bd909743b339133c--b6ef37b9bc834cebb380fa9de064021b f51a2caa8d824b329e036b609360568b t = 10 b6ef37b9bc834cebb380fa9de064021b--f51a2caa8d824b329e036b609360568b f51a2caa8d824b329e036b609360568b--5e0abc0184ac4bc5a27108d82bc20e25 4478451489d840769ac5caad36ebbe15 56e67fde613945a9ad1ebb32f99d292e RX(0.5) 625ea252d483459f98353b038d78d14b--56e67fde613945a9ad1ebb32f99d292e e771cd2fd063461ba515f6b4e0db445a X 56e67fde613945a9ad1ebb32f99d292e--e771cd2fd063461ba515f6b4e0db445a e771cd2fd063461ba515f6b4e0db445a--b6ef37b9bc834cebb380fa9de064021b dd42ab52932c451cbd992087e344c8b4 e771cd2fd063461ba515f6b4e0db445a--dd42ab52932c451cbd992087e344c8b4 dd42ab52932c451cbd992087e344c8b4--4478451489d840769ac5caad36ebbe15
from qadence import feature_map, hea, chain

block = chain(feature_map(4, fm_type="tower"), hea(4,2))
%3 cluster_70ce1e429549474986a336ae4a84e5e9 HEA cluster_186dda4d94a84d6dbf89c7bff83e3abd FM 4b16a31900174cdabb2a9e30fc020d55 0 5f6d338a40664c8896c54830835481cd RX(2*acos(phi)) 4b16a31900174cdabb2a9e30fc020d55--5f6d338a40664c8896c54830835481cd e9f5815442ac45c7aceb37d85f227178 1 f568dc091d6d4aa3b7fa7a19fa2aaab7 RX(theta₀) 5f6d338a40664c8896c54830835481cd--f568dc091d6d4aa3b7fa7a19fa2aaab7 ea8c27a26a7f4e21b4b6fca208ca3b52 RY(theta₄) f568dc091d6d4aa3b7fa7a19fa2aaab7--ea8c27a26a7f4e21b4b6fca208ca3b52 d28990e8dc6540808098d6f58001e008 RX(theta₈) ea8c27a26a7f4e21b4b6fca208ca3b52--d28990e8dc6540808098d6f58001e008 4fa0409ce5504a2db5dafe160eb466f3 d28990e8dc6540808098d6f58001e008--4fa0409ce5504a2db5dafe160eb466f3 775a9f63128145528e699793f88b9f65 4fa0409ce5504a2db5dafe160eb466f3--775a9f63128145528e699793f88b9f65 21e4275d1ed0402e991624081a09aa6d RX(theta₁₂) 775a9f63128145528e699793f88b9f65--21e4275d1ed0402e991624081a09aa6d ba0f85670e154d8daaf46a0e4611c2a7 RY(theta₁₆) 21e4275d1ed0402e991624081a09aa6d--ba0f85670e154d8daaf46a0e4611c2a7 5efc87a8e38f4ddc8c2255e135dd7295 RX(theta₂₀) ba0f85670e154d8daaf46a0e4611c2a7--5efc87a8e38f4ddc8c2255e135dd7295 a677f5a396574bdfac0c3a2e66d412a7 5efc87a8e38f4ddc8c2255e135dd7295--a677f5a396574bdfac0c3a2e66d412a7 76b3a5ff7aa14258980a0303e961c1bc a677f5a396574bdfac0c3a2e66d412a7--76b3a5ff7aa14258980a0303e961c1bc ff1d2fa0956e4b539fa67a94dcf1233c 76b3a5ff7aa14258980a0303e961c1bc--ff1d2fa0956e4b539fa67a94dcf1233c 5fe6a417e96a4e9d9e6e7e4c0b3caa3e 7bb34908008c40b3998c6a13bb0eb7e5 RX(4*acos(phi)) e9f5815442ac45c7aceb37d85f227178--7bb34908008c40b3998c6a13bb0eb7e5 d8a4596e48de46f5a18964d0f32fab38 2 e90ae3ef11794785ab549b214f48e970 RX(theta₁) 7bb34908008c40b3998c6a13bb0eb7e5--e90ae3ef11794785ab549b214f48e970 0ddeb94d384c4e59812d156d88ae7ed7 RY(theta₅) e90ae3ef11794785ab549b214f48e970--0ddeb94d384c4e59812d156d88ae7ed7 78a65398ff564967b68432d55bacee14 RX(theta₉) 0ddeb94d384c4e59812d156d88ae7ed7--78a65398ff564967b68432d55bacee14 117a8ee5f1b24fec92e217d1620cc254 X 78a65398ff564967b68432d55bacee14--117a8ee5f1b24fec92e217d1620cc254 117a8ee5f1b24fec92e217d1620cc254--4fa0409ce5504a2db5dafe160eb466f3 d37b4771f469405c999fd9d017fba773 117a8ee5f1b24fec92e217d1620cc254--d37b4771f469405c999fd9d017fba773 b229add32771423684748515922a391f RX(theta₁₃) d37b4771f469405c999fd9d017fba773--b229add32771423684748515922a391f 08865c0138fa43cf8d91026a505a0ab9 RY(theta₁₇) b229add32771423684748515922a391f--08865c0138fa43cf8d91026a505a0ab9 abc8f61f59934becb5ec11a4ee0114b3 RX(theta₂₁) 08865c0138fa43cf8d91026a505a0ab9--abc8f61f59934becb5ec11a4ee0114b3 a5df8f8100564ba28fdbb0d8b156dd10 X abc8f61f59934becb5ec11a4ee0114b3--a5df8f8100564ba28fdbb0d8b156dd10 a5df8f8100564ba28fdbb0d8b156dd10--a677f5a396574bdfac0c3a2e66d412a7 d85310693da54fa5ba293f69c4f792c8 a5df8f8100564ba28fdbb0d8b156dd10--d85310693da54fa5ba293f69c4f792c8 d85310693da54fa5ba293f69c4f792c8--5fe6a417e96a4e9d9e6e7e4c0b3caa3e b1f6394f84be44a18bbf0e95b1422f88 5b48c9fdc95745c59fd0038f0f4c6e0b RX(6*acos(phi)) d8a4596e48de46f5a18964d0f32fab38--5b48c9fdc95745c59fd0038f0f4c6e0b 0b8da4d0846e4358a8b4c6f6ecbdd9d1 3 e89f8337da234297bdb70e801c7e48e0 RX(theta₂) 5b48c9fdc95745c59fd0038f0f4c6e0b--e89f8337da234297bdb70e801c7e48e0 de7f441b9dd94c9c9e387d2fe75511f9 RY(theta₆) e89f8337da234297bdb70e801c7e48e0--de7f441b9dd94c9c9e387d2fe75511f9 a2c2955d92094c8a82d16d80605a75d2 RX(theta₁₀) de7f441b9dd94c9c9e387d2fe75511f9--a2c2955d92094c8a82d16d80605a75d2 52c0992ffa0c404abe3624eb68853dc5 a2c2955d92094c8a82d16d80605a75d2--52c0992ffa0c404abe3624eb68853dc5 0e0bbbc583e640c291c0d72fe9d6e863 X 52c0992ffa0c404abe3624eb68853dc5--0e0bbbc583e640c291c0d72fe9d6e863 0e0bbbc583e640c291c0d72fe9d6e863--d37b4771f469405c999fd9d017fba773 49fc20d6865a4336a9ee53fd564385a7 RX(theta₁₄) 0e0bbbc583e640c291c0d72fe9d6e863--49fc20d6865a4336a9ee53fd564385a7 a4a7a13bcd3c495482ce4a3a4ada3fdf RY(theta₁₈) 49fc20d6865a4336a9ee53fd564385a7--a4a7a13bcd3c495482ce4a3a4ada3fdf edd6874a9ea8473f9076670fc13dd7bf RX(theta₂₂) a4a7a13bcd3c495482ce4a3a4ada3fdf--edd6874a9ea8473f9076670fc13dd7bf c1a76a6f54ab4cd09f94ea4059a13411 edd6874a9ea8473f9076670fc13dd7bf--c1a76a6f54ab4cd09f94ea4059a13411 21dbcd511f3b4ea3be9c00a8f5e8f971 X c1a76a6f54ab4cd09f94ea4059a13411--21dbcd511f3b4ea3be9c00a8f5e8f971 21dbcd511f3b4ea3be9c00a8f5e8f971--d85310693da54fa5ba293f69c4f792c8 21dbcd511f3b4ea3be9c00a8f5e8f971--b1f6394f84be44a18bbf0e95b1422f88 1f7e8ac0dc524d609af94f4a1d38c6b2 ab406fcd52364cd0835434d4e6055f53 RX(8*acos(phi)) 0b8da4d0846e4358a8b4c6f6ecbdd9d1--ab406fcd52364cd0835434d4e6055f53 e556031d1e7d4472a5e7261503d9a122 RX(theta₃) ab406fcd52364cd0835434d4e6055f53--e556031d1e7d4472a5e7261503d9a122 90a26f38af1c4ad4b6e98c28730734e0 RY(theta₇) e556031d1e7d4472a5e7261503d9a122--90a26f38af1c4ad4b6e98c28730734e0 2ed939d7794e42499e6b62aebc8b628e RX(theta₁₁) 90a26f38af1c4ad4b6e98c28730734e0--2ed939d7794e42499e6b62aebc8b628e feabb528dd3742f38127d312c956404f X 2ed939d7794e42499e6b62aebc8b628e--feabb528dd3742f38127d312c956404f feabb528dd3742f38127d312c956404f--52c0992ffa0c404abe3624eb68853dc5 19029927885649ceab0afb35f4f1f251 feabb528dd3742f38127d312c956404f--19029927885649ceab0afb35f4f1f251 bd25cab76b434a928b10587393fa84bd RX(theta₁₅) 19029927885649ceab0afb35f4f1f251--bd25cab76b434a928b10587393fa84bd 25c068e65aa542809816945cdb6f9033 RY(theta₁₉) bd25cab76b434a928b10587393fa84bd--25c068e65aa542809816945cdb6f9033 d612b14f72e24df98c3c7d0349f190fb RX(theta₂₃) 25c068e65aa542809816945cdb6f9033--d612b14f72e24df98c3c7d0349f190fb 9ca94b6419a446c59cecb96f20bd2650 X d612b14f72e24df98c3c7d0349f190fb--9ca94b6419a446c59cecb96f20bd2650 9ca94b6419a446c59cecb96f20bd2650--c1a76a6f54ab4cd09f94ea4059a13411 b565e36ab39d45bfb0aa2f3da3f629cf 9ca94b6419a446c59cecb96f20bd2650--b565e36ab39d45bfb0aa2f3da3f629cf b565e36ab39d45bfb0aa2f3da3f629cf--1f7e8ac0dc524d609af94f4a1d38c6b2

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