#!/usr/bin/env python
# coding: utf-8

# In[1]:


get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
import networkx as nx
from importlib import reload
import os
import autodepgraph as adg
from autodepgraph import  AutoDepGraph_DAG


# In[2]:


cal_True_delayed =  'autodepgraph.node_functions.calibration_functions.test_calibration_True_delayed'
test_graph = AutoDepGraph_DAG('test graph')
for node in ['A', 'B', 'C', 'D', 'E']:
    test_graph.add_node(node,
                        calibrate_function=cal_True_delayed)


# In[3]:


get_ipython().run_line_magic('pinfo', 'test_graph.add_node')


# In[4]:


test_graph.add_edge('C', 'A')
test_graph.add_edge('C', 'B')
test_graph.add_edge('B', 'A')
test_graph.add_edge('D', 'A')
test_graph.add_edge('E', 'D')


# In[5]:


# The default plotting mode is SVG
test_graph.cfg_plot_mode = 'svg'
# Updates the monitor, in this case the svg/html page
test_graph.update_monitor()

# Updating the monitor overwrites an svg file whose location is determined by the attribute:
test_graph.cfg_svg_filename


# In[6]:


from IPython.display import display, SVG
display(SVG(test_graph.cfg_svg_filename))


# In[7]:


# The html page is located at the location specified by the url.
# The page generated based on a template when the open_html_viewer command is called.
# url = test_graph.open_html_viewer()
# print(url)


# In[8]:


# Alternatively a render in matplotlib can be drawn
test_graph.draw_mpl()


# In[9]:


test_graph.set_all_node_states('needs calibration')


# In[10]:


test_graph.maintain_B()

display(SVG(test_graph.cfg_svg_filename))


# In[11]:


# Update the plotting monitor (default matplotlib) to show your graph
test_graph.update_monitor()


# In[12]:


test_graph.set_all_node_states('needs calibration')

test_graph.maintain_node('E')

display(SVG(test_graph.cfg_svg_filename))


# In[13]:


test_dir = os.path.join(adg.__path__[0], 'tests', 'test_data')
fn = os.path.join(test_dir, 'three_qubit_graph.yaml')
DAG = nx.readwrite.read_yaml(fn)


# In[14]:


test_graph.cfg_plot_mode = 'svg'
DAG.update_monitor()
# This graph is so big, the html visualization is more suitable.
display(SVG(DAG.cfg_svg_filename))

