import io
from omegaconf import OmegaConf
import pp
from pp.config import connections

c0 = pp.c.mzi()
pp.qp(c0)

netlist = c0.get_netlist()

"""
Explanation: YAML netlist export
Any component exports its netlist get_netlist and returns an OmegaConf dict that can be easily converted into JSON and YAML
End of explanation
"""


import pp

c1 = pp.component_from_yaml('mzi.yml')
pp.show(c1)
pp.plotgds(c1)

len(c1.get_netlist().connections)

"""
Explanation: This is what we did to store the netlist
OmegaConf.save(netlist, 'mzi.yml')
connections = []  # reset the connections global variable
To be even safer (connections are stored in a global variable) you can restart your Jupyter Kernel
End of explanation
"""


connections.clear()  # reset the connections global variable

import pp

c2 = pp.component_from_yaml('mzi2.yml')
pp.show(c2)
pp.plotgds(c2)

len(c2.get_netlist().connections)

"""
Explanation: You can also reconstruct the circuit from the instances and placements but the you lose the most important part of the netlist, which is the connections
This is what we did to store only instances and placements
```
netlist = OmegaConf.create(dict(instances=netlist.instances, placements=netlist.placements))
OmegaConf.save(netlist, 'mzi2.yml')
```
End of explanation
"""


connections.clear()  # reset the connections global variable

import pp

c3 = pp.component_from_yaml('mzi3.yml')
pp.show(c3)
pp.plotgds(c3)

len(c3.get_netlist().connections)

"""
Explanation: A better way to reconstruct the circuit and maintain connectivity is from the instances and connections
This is what we did to store only instances and placements
c0 = pp.c.mzi()
netlist = c0.get_netlist()
netlist = OmegaConf.create(dict(instances=netlist.instances, connections=netlist.connections))
OmegaConf.save(netlist, 'mzi3.yml')
End of explanation
"""


