QuantestPy / quantestpy

Apache License 2.0
9 stars 3 forks source link

Add draw method in QuantestPyCircuit and StateVectorCircuit #183

Closed junnaka51 closed 1 year ago

junnaka51 commented 1 year ago

In this PR I implement the circuit drawer classes for QuantestPyCircuit, StateVectorCircuit and PauliCircuit.

These classes may be accessed by users via draw() methods:

In [2]: qc = QuantestPyCircuit(3)
   ...: qc.add_gate({"name": "h", "control_qubit": [], "target_qubit": [0],
   ...:             "control_value": [], "parameter": []})
   ...: qc.add_gate({"name": "x", "control_qubit": [0], "target_qubit": [2],
   ...:             "control_value": [1], "parameter": []})
   ...: qc.add_gate({"name": "ry", "control_qubit": [], "target_qubit": [1],
   ...:             "control_value": [], "parameter": [np.pi/6.]})
   ...: qc.draw()
Out[2]: 
0 ─[H]─■─────── 0
       │         
1 ─────┼─[R_y]─ 1
       │         
2 ────[X]────── 2

In [3]: 
In [3]: svc = StateVectorCircuit(3)
   ...: svc.add_gate({"name": "h", "control_qubit": [], "target_qubit": [1],
   ...:               "control_value": [], "parameter": []})
   ...: svc.add_gate({"name": "x", "control_qubit": [1], "target_qubit": [2],
   ...:               "control_value": [1], "parameter": []})
   ...: svc.add_gate({"name": "ry", "control_qubit": [2], "target_qubit": [0],
   ...:               "control_value": [0], "parameter": [np.pi/6.]})
   ...: svc.draw()
Out[3]: 
0 ───────[R_y]─ 0
           │     
1 ─[H]─■───┼─── 1
       │   │     
2 ────[X]──o─── 2

In [4]:
In [7]: pc = PauliCircuit(3)
   ...: pc.add_gate({"name": "y", "control_qubit": [], "target_qubit": [1],
   ...:             "control_value": [], "parameter": []})
   ...: pc.add_gate({"name": "x", "control_qubit": [1], "target_qubit": [2],
   ...:             "control_value": [1], "parameter": []})
   ...: pc.add_gate({"name": "x", "control_qubit": [1, 2], "target_qubit": [0],
   ...:             "control_value": [1, 1]})
   ...: pc.draw()
Out[7]: 
0 |0> 0.0 ───────[X]─ |1> 0.0 0
                  │            
1 |0> 0.0 ─[Y]─■──■── |1> 0.5 1
               │  │            
2 |0> 0.0 ────[X]─■── |1> 0.0 2

In [8]: 

image

PauliCircuitDrawer class is more special than the others in the sense that it is intended to be used in assert methods at the moment.

junnaka51 commented 1 year ago

TODO

junnaka51 commented 1 year ago

@mitz1012 As I mentioned, I completed adding a few test files. I am sorry but I have realized a small issue and accordingly done a tiny modification, too 👉 https://github.com/QuantestPy/quantestpy/pull/183/commits/447c2a0aba5055e1e1940faf9795a5b4858700ae Thank you so much for your review in advance!