Grv
In [1]:
Copied!
import graphviz
# 创建一个有向图,设置样式使其更像一张“蓝图”
dot = graphviz.Digraph(comment='Quantum Circuit Blueprint',
graph_attr={'rankdir': 'LR', 'splines': 'ortho', 'nodesep': '0.5'},
node_attr={'shape': 'box', 'style': 'rounded,filled', 'fillcolor': 'lightgrey', 'fontname': 'Arial'},
edge_attr={'arrowsize': '0.7'})
# 1. 添加节点
# 量子比特初始态节点
dot.node('q0_init', '|0⟩\nQubit 0')
dot.node('q1_init', '|0⟩\nQubit 1')
# 量子门节点
dot.node('H_gate', 'H Gate', {'shape': 'ellipse', 'fillcolor': 'lightblue'})
dot.node('CNOT_gate', 'CNOT Gate', {'shape': 'ellipse', 'fillcolor': 'lightcoral'})
# 测量节点
dot.node('M0', 'Measure', {'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'})
dot.node('M1', 'Measure', {'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'})
# 经典比特节点
dot.node('c0', 'c[0]', {'shape': 'note', 'fillcolor': 'honeydew'})
dot.node('c1', 'c[1]', {'shape': 'note', 'fillcolor': 'honeydew'})
# 2. 添加连线(定义数据流)
# 第一比特线路
dot.edges([('q0_init', 'H_gate'), ('H_gate', 'CNOT_gate')])
# 第二比特线路
dot.edge('q1_init', 'CNOT_gate')
# 测量连线
dot.edge('CNOT_gate', 'M0')
dot.edge('CNOT_gate', 'M1')
# 测量结果输出到经典寄存器
dot.edge('M0', 'c0', **{'style': 'dashed', 'color': 'grey'})
dot.edge('M1', 'c1', **{'style': 'dashed', 'color': 'grey'})
# 控制连线(用虚线表示),例如CNOT的控制关系
dot.edge('H_gate', 'CNOT_gate', **{'style': 'dashed', 'color': 'blue', 'label': 'control', 'constraint': 'false'})
# 3. 渲染并保存图片
blueprint_file = dot.render(filename='quantum_circuit_blueprint', format='png', cleanup=True, view=False)
print(f"蓝图式示意图已生成: {blueprint_file}")
import graphviz
# 创建一个有向图,设置样式使其更像一张“蓝图”
dot = graphviz.Digraph(comment='Quantum Circuit Blueprint',
graph_attr={'rankdir': 'LR', 'splines': 'ortho', 'nodesep': '0.5'},
node_attr={'shape': 'box', 'style': 'rounded,filled', 'fillcolor': 'lightgrey', 'fontname': 'Arial'},
edge_attr={'arrowsize': '0.7'})
# 1. 添加节点
# 量子比特初始态节点
dot.node('q0_init', '|0⟩\nQubit 0')
dot.node('q1_init', '|0⟩\nQubit 1')
# 量子门节点
dot.node('H_gate', 'H Gate', {'shape': 'ellipse', 'fillcolor': 'lightblue'})
dot.node('CNOT_gate', 'CNOT Gate', {'shape': 'ellipse', 'fillcolor': 'lightcoral'})
# 测量节点
dot.node('M0', 'Measure', {'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'})
dot.node('M1', 'Measure', {'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'})
# 经典比特节点
dot.node('c0', 'c[0]', {'shape': 'note', 'fillcolor': 'honeydew'})
dot.node('c1', 'c[1]', {'shape': 'note', 'fillcolor': 'honeydew'})
# 2. 添加连线(定义数据流)
# 第一比特线路
dot.edges([('q0_init', 'H_gate'), ('H_gate', 'CNOT_gate')])
# 第二比特线路
dot.edge('q1_init', 'CNOT_gate')
# 测量连线
dot.edge('CNOT_gate', 'M0')
dot.edge('CNOT_gate', 'M1')
# 测量结果输出到经典寄存器
dot.edge('M0', 'c0', **{'style': 'dashed', 'color': 'grey'})
dot.edge('M1', 'c1', **{'style': 'dashed', 'color': 'grey'})
# 控制连线(用虚线表示),例如CNOT的控制关系
dot.edge('H_gate', 'CNOT_gate', **{'style': 'dashed', 'color': 'blue', 'label': 'control', 'constraint': 'false'})
# 3. 渲染并保存图片
blueprint_file = dot.render(filename='quantum_circuit_blueprint', format='png', cleanup=True, view=False)
print(f"蓝图式示意图已生成: {blueprint_file}")
C:\Users\Administrator\AppData\Local\Temp\ipykernel_21784\526202679.py:14: DeprecationWarning: The signature of node will be reduced to 2 positional args (ignoring self)) ['self', 'name', 'label']: pass _attributes={'shape': 'ellipse', 'fillcolor': 'lightblue'} as keyword args
dot.node('H_gate', 'H Gate', {'shape': 'ellipse', 'fillcolor': 'lightblue'})
C:\Users\Administrator\AppData\Local\Temp\ipykernel_21784\526202679.py:15: DeprecationWarning: The signature of node will be reduced to 2 positional args (ignoring self)) ['self', 'name', 'label']: pass _attributes={'shape': 'ellipse', 'fillcolor': 'lightcoral'} as keyword args
dot.node('CNOT_gate', 'CNOT Gate', {'shape': 'ellipse', 'fillcolor': 'lightcoral'})
C:\Users\Administrator\AppData\Local\Temp\ipykernel_21784\526202679.py:17: DeprecationWarning: The signature of node will be reduced to 2 positional args (ignoring self)) ['self', 'name', 'label']: pass _attributes={'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'} as keyword args
dot.node('M0', 'Measure', {'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'})
C:\Users\Administrator\AppData\Local\Temp\ipykernel_21784\526202679.py:18: DeprecationWarning: The signature of node will be reduced to 2 positional args (ignoring self)) ['self', 'name', 'label']: pass _attributes={'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'} as keyword args
dot.node('M1', 'Measure', {'shape': 'parallelogram', 'fillcolor': 'lemonchiffon'})
C:\Users\Administrator\AppData\Local\Temp\ipykernel_21784\526202679.py:20: DeprecationWarning: The signature of node will be reduced to 2 positional args (ignoring self)) ['self', 'name', 'label']: pass _attributes={'shape': 'note', 'fillcolor': 'honeydew'} as keyword args
dot.node('c0', 'c[0]', {'shape': 'note', 'fillcolor': 'honeydew'})
C:\Users\Administrator\AppData\Local\Temp\ipykernel_21784\526202679.py:21: DeprecationWarning: The signature of node will be reduced to 2 positional args (ignoring self)) ['self', 'name', 'label']: pass _attributes={'shape': 'note', 'fillcolor': 'honeydew'} as keyword args
dot.node('c1', 'c[1]', {'shape': 'note', 'fillcolor': 'honeydew'})
Warning: Orthogonal edges do not currently handle edge labels. Try using xlabels.
蓝图式示意图已生成: quantum_circuit_blueprint.png
In [ ]:
Copied!