Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circuit drawer #75

Merged
merged 14 commits into from
Jul 14, 2023
Merged

Circuit drawer #75

merged 14 commits into from
Jul 14, 2023

Conversation

wakuwaku414
Copy link
Contributor

@wakuwaku414 wakuwaku414 commented Apr 19, 2023

Added circuit visualizer based on qulacs-visualizer.
Note that current implementation only support NonParametricQuantumCircuit.
↑ Updated ee3eaa8

examples:

from quri_parts.circuit import QuantumCircuit
from quri_parts.circuit.utils.circuit_drawer import draw_circuit

circuit = QuantumCircuit(3)
circuit.add_X_gate(0)
circuit.add_Y_gate(1)
circuit.add_RZ_gate(0, 0.1)
circuit.add_Z_gate(0)
circuit.add_SWAP_gate(0, 2)
circuit.add_CNOT_gate(2, 1)
circuit.add_UnitaryMatrix_gate(
    target_indices=(0, 1),
    unitary_matrix=[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],
)
circuit.add_PauliRotation_gate((0, 2), (0, 2), (0.1, 0.1))

draw_circuit(circuit)
   ___     ___     ___                     ___     ___   
  | X |   |RZ |   | Z |    4              |Mat|   |PR |  
--|0  |---|2  |---|3  |-----x-------------|6  |---|7  |--
  |___|   |___|   |___|     |             |   |   |_ _|  
   ___                      |      ___    |   |    | |   
  | Y |                     |     |CX |   |   |    | |   
--|1  |---------------------|-----|5  |---|   |----| |---
  |___|                     |     |___|   |___|    | |   
                            |       |             _| |_  
                            |       |             |   |  
----------------------------x-------●-------------|   |--
                                                  |___|  

@github-actions
Copy link

github-actions bot commented Apr 19, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@wakuwaku414
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@kwkbtr
Copy link
Contributor

kwkbtr commented May 10, 2023

Thank you for a cool feature!
I found cases where the drawings are not correct (ideal).
They are cases where a gate exists at "the same depth" as a two-qubit gate and acts on a qubit between qubits acted by the two-qubit gate.

# Both drawn, collided
>>> c = QuantumCircuit(3)
>>> c.add_SWAP_gate(0, 2)
>>> c.add_X_gate(1)
>>> draw_circuit(c)

   0
----x------------
    |
    ___
   | X |
---|1  |---------
   |___|
    |
    |
----x------------

# Only the two-qubit gate is drawn
>>> c = QuantumCircuit(3)
>>> c.add_X_gate(1)
>>> c.add_SWAP_gate(0, 2)
>>> draw_circuit(c)

    1
-----x-----------
     |
     |
     |
-----|-----------
     |
     |
     |
-----x-----------

# Both drawn, collided
>>> c = QuantumCircuit(3)
>>> c.add_CNOT_gate(0, 2)
>>> c.add_X_gate(1)
>>> draw_circuit(c)


----●------------
    |
    ___
   | X |
---|1  |---------
   |___|
   _|_
  |CX |
--|0  |----------
  |___|

# Only the two-qubit gate is drawn
>>> c = QuantumCircuit(3)
>>> c.add_X_gate(1)
>>> c.add_CNOT_gate(0, 2)
>>> draw_circuit(c)


-----●-----------
     |
     |
     |
-----|-----------
     |
    _|_
   |CX |
---|1  |---------
   |___|

@wakuwaku414
Copy link
Contributor Author

Thank you for your response.
I will try to fix it!

@wakuwaku414
Copy link
Contributor Author

Now it works correctly!
Here are the example outputs:

>>> c = QuantumCircuit(3)
>>> c.add_SWAP_gate(0, 2)
>>> c.add_X_gate(1)
>>> draw_circuit(c)
                
   0            
----x-----------
    |           
    |     ___   
    |    | X |  
----|----|1  |--
    |    |___|  
    |           
    |           
----x-----------
                


>>> c = QuantumCircuit(3)
>>> c.add_X_gate(1)
>>> c.add_SWAP_gate(0, 2)
>>> draw_circuit(c)
                
          1     
-----------x----
           |    
   ___     |    
  | X |    |    
--|0  |----|----
  |___|    |    
           |    
           |    
-----------x----
                


>>> c = QuantumCircuit(3)
>>> c.add_CNOT_gate(0, 2)
>>> c.add_X_gate(1)
>>> draw_circuit(c)
                
                
----●-----------
    |           
    |     ___   
    |    | X |  
----|----|1  |--
    |    |___|  
   _|_          
  |CX |         
--|0  |---------
  |___|         



>>> c = QuantumCircuit(3)
>>> c.add_X_gate(1)
>>> c.add_CNOT_gate(0, 2)
>>> draw_circuit(c)
                
                
-----------●----
           |    
   ___     |    
  | X |    |    
--|0  |----|----
  |___|    |    
          _|_   
         |CX |  
---------|1  |--
         |___|  



>>> c = QuantumCircuit(3)
>>> c.add_X_gate(0)
>>> c.add_CNOT_gate(0, 2)
>>> c.add_X_gate(1)
>>> draw_circuit(c)
   ___          
  | X |         
--|0  |-----●---
  |___|     |   
   ___      |   
  | X |     |   
--|2  |-----|---
  |___|     |   
           _|_  
          |CX | 
----------|1  |-
          |___| 



>>> c = QuantumCircuit(3)
>>> c.add_X_gate(0)
>>> c.add_RZ_gate(0, 0.1)
>>> c.add_SWAP_gate(0, 2)
>>> c.add_PauliRotation_gate((0, 2), (0, 2), (0.1, 0.1))
>>> c.add_X_gate(1)
>>> draw_circuit(c)
   ___     ___             ___  
  | X |   |RZ |    2      |PR | 
--|0  |---|1  |-----x-----|3  |-
  |___|   |___|     |     |_ _| 
   ___              |      | |  
  | X |             |      | |  
--|4  |-------------|------| |--
  |___|             |      | |  
                    |     _| |_ 
                    |     |   | 
--------------------x-----|   |-
                          |___| 



>>> hwe = HardwareEfficient(3, 2)
>>> draw_circuit(hwe)
   ___     ___                     ___     ___                     ___     ___  
  |PRY|   |PRZ|                   |PRY|   |PRZ|                   |PRY|   |PRZ| 
--|0  |---|1  |-------------●-----|8  |---|9  |-------------●-----|16 |---|17 |-
  |___|   |___|             |     |___|   |___|             |     |___|   |___| 
   ___     ___             _|_     ___     ___             _|_     ___     ___  
  |PRY|   |PRZ|           |CZ |   |PRY|   |PRZ|           |CZ |   |PRY|   |PRZ| 
--|2  |---|3  |-----●-----|7  |---|10 |---|11 |-----●-----|15 |---|18 |---|19 |-
  |___|   |___|     |     |___|   |___|   |___|     |     |___|   |___|   |___| 
   ___     ___     _|_     ___     ___             _|_     ___     ___          
  |PRY|   |PRZ|   |CZ |   |PRY|   |PRZ|           |CZ |   |PRY|   |PRZ|         
--|4  |---|5  |---|6  |---|12 |---|13 |-----------|14 |---|20 |---|21 |---------
  |___|   |___|   |___|   |___|   |___|           |___|   |___|   |___|         

Copy link
Contributor

@kwkbtr kwkbtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be a problem in the "devide and fold" section.

from quri_parts.circuit import QuantumCircuit
from quri_parts.circuit.utils.circuit_drawer import draw_circuit

c = QuantumCircuit(4)
c.add_X_gate(1)
c.add_CNOT_gate(0, 3)
c.add_SWAP_gate(0, 2)
c.add_X_gate(1)

draw_circuit(c, 32)

"""
-----------●--------x-----------
           |        |
   ___     |        |     ___
  | X |    |        |    | X |
--|0  |----|--------|----|3  |--
  |___|    |        |    |___|
           |        |
           |        |
-----------|--------x-----------
           |
          _|_
         |CX |
---------|1  |------------------
         |___|
"""

draw_circuit(c, 24)

"""
                   2
-----------●--------x---
           |        |
   ___     |        |
  | X |    |        |
--|0  |----|--------|---
  |___|    |        |
           |        |
           |        |
-----------|--------x---
           |
          _|_
         |CX |
---------|1  |----------
         |___|
"""

A part of the circuit is truncated when the line_length is short.

Copy link
Contributor

@kwkbtr kwkbtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@kwkbtr kwkbtr merged commit 8625111 into QunaSys:main Jul 14, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jul 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants