Visually editing circuits with cirq
Cirq
is a package for creating and editing circuits of arbitrary domain.
The very simple data structure allows for interfacing with further modeling and simulation backends.
Circuits, i.e., abstract networks of interconnected components with ports, have found application in various scientific and engineering domains, ranging from applications close to the physical implementation, such as electrical circuits, photonic circuits for optical information processing, superconducting circuits for quantum information applications to more abstract circuit representations of dynamical systems, modeling biological processes or even software algorithms.
Their great applicability has already led to the development of many domain-specific modeling and simulation toolkits as well as some very general domain-independent toolkits such as Modelica, but to date, there exist very few open source graphical general circuit editing environments that can be tightly integrated with custom, domain-specific implementation simulation or analysis backends as well as IPython.
An in-browser visual circuit editor leads to a rich integrated simulation and analysis workflow in which an engineer or researcher can receive very fast feedback when making changes to his model. As a consequence, it is much easier to build intuition for the particular kinds of circuit models and find novel and creative solutions to an engineering task.
In [1]:
import cirq; reload(cirq);
from cirq import *
init_js()
In [2]:
import cirq.tests; reload(cirq.tests);
cirq.tests.test_mach_zehnder()
Let's assume an example from my field, the connections are given by directed propagating light fields.
Directed connections are called causal
. Moreover, each input can only be connected to a single output
due to reasons of unitarity of the underlying physics. All energy/information must be accounted for.
This is indicated by the one2one
keyword, which only applies to domains with causal=True
.
To provide an additional example, we also define an electrical domain, which is undirected/acausal and we will draw its ports and connections in purple.
In [3]:
fm = Domain(name="fieldmode", causal=True, one2one=True)
el = Domain(name="electrical", causal=False, _color="purple")
fm, el
Out[3]:
In [4]:
Inputs = [Port(name="In{}".format(k+1), domain=fm, direction="in") for k in range(5)]
Outputs = [Port(name="Out{}".format(k+1), domain=fm, direction="out") for k in range(5)]
el_port = Port(name="Control", domain=el, direction="inout")
Inputs, Outputs, el_port
Out[4]:
In [5]:
BS = ComponentType(name="Beamsplitter", ports=clone_ports(Inputs[:2]+Outputs[:2]))
Phase = ComponentType(name="Phase", ports=clone_ports(Inputs[:1]+[el_port]+Outputs[:1]))
In [6]:
b1, b2 = map(BS.make_instance, ["b1", "b2"])
phi = Phase.make_instance("phi")
mz = Circuit(name="MachZehnder",
ports=clone_ports(Inputs[:2]+[el_port]+Outputs[:2]),
component_instances=[b1,b2,phi])
mz.connections = [Connection(source=s, target=t)
for (s,t) in [
(mz.p.In1, b1.p.In1),
(mz.p.In2, b1.p.In2),
(b1.p.Out1, phi.p.In1),
(b1.p.Out2, b2.p.In1),
(phi.p.Out1, b2.p.In2),
(b2.p.Out1, mz.p.Out1),
(b2.p.Out2, mz.p.Out2),
(mz.p.Control, phi.p.Control)]]
mz
In [7]:
from IPython.html.widgets import interact
@interact(r=(20,200))
def resize_b1(r=ComponentInstance._r.default_value):
b1._r = r
b1.layout_ports(b1.ports)
In [8]:
cb = CircuitBuilder([fm, el], [BS, Phase], mz)
cb
In [9]:
from IPython.display import Image, FileLink, SVG, display
In [10]:
cb.circuit.capture_svg()
In [11]:
cb.circuit.save_last_image("mach_zehnder.svg")
cb.circuit.save_last_image("mach_zehnder.png")
cb.circuit.save_last_image("mach_zehnder.pdf")
In [12]:
# this needs to run in another cell, because the above method writes the SVG file asynchronously
display(SVG(filename="mach_zehnder.svg"),
Image(filename="mach_zehnder.png"),
FileLink("mach_zehnder.pdf"))
In [13]:
mz.to_jsonifiable()
Out[13]:
In [14]:
mz2 = Circuit.from_jsonifiable(mz.to_jsonifiable())
In [15]:
mz2
In [16]:
mz.get_nets(fm)
Out[16]:
In [17]:
mz.get_nets(el)
Out[17]: