QuTech-Delft / OpenQL

OpenQL: A Portable Quantum Programming Framework for Quantum Accelerators. https://dl.acm.org/doi/10.1145/3474222
https://openql.readthedocs.io
Other
100 stars 44 forks source link

Visualizer #354

Closed tmeer closed 4 years ago

tmeer commented 4 years ago

Resolved Jeroen's comments from last pull request:

Visualizer usage

The visualizer can be ran by adding the visualizer pass to the compiler and compiling the quantum program you want to run. Python example:

c = ql.Compiler("example_compiler")
c.add_pass("Visualizer");
c.set_pass_option("Visualizer", "visualizer_config_path", "full_path_to_file"));

# add program and gates

c.compile(program)

The general visualization parameters are read from a configuration file. The path to this file should be added through a pass option for the visualizer as seen above. If a parameter is missing from that file, the default hardcoded value contained in src\visualizer.h will be used instead.

Custom gates

When using custom gates the default gate visualizations are not used and the visualization needs to be defined by the user. Take for example the following custom Hadamard gate defined on qubit 1:

"h q1": {
  "duration": 40,
  "latency": 0,
  "qubits": ["q1"],
  "matrix": [ [0.0,1.0], [1.0,0.0], [1.0,0.0], [0.0,0.0] ],
  "disable_optimization": false,
  "type": "mw",
  "cc_light_instr_type": "single_qubit_gate",
  "cc_light_instr": "h",
  "cc_light_codeword": 91,
  "cc_light_opcode": 9,
  "visual": {
    "connectionColor": [0, 0, 0],
    "nodes": [
      {
        "type": "GATE",
        "radius": 13,
        "displayName": "H",
        "fontHeight": 13,
        "fontColor": [255, 255, 255],
        "backgroundColor": [70, 210, 230],
        "outlineColor": [70, 210, 230]
      }
    ]
  }
}

Note the new "visual" attribute, which defines the look of the custom gate. Each gate has a connectionColor which defines the color of the connection line for multi-operand gates, and an array of 'nodes'. A node is the visualization of the gate acting on a specific qubit or classical bit. If a Hadamard gate is acting on qubit 3, that is represented by one node. If a CNOT gate is acting on qubits 1 and 2, it will have two nodes, one describing the visualization of the CNOT gate at qubit 1 and one describing the visualization on qubit 2. A measurement gate measuring qubit 5 and storing the result in classical bit 0 will again have two nodes.

Each node has several attributes describing its visualization:

The colors are defined as RGB arrays: [R, G, B].

The type of the nodes can be one of the following:

When a gate has multiple operands, each operand should have a node associated with it. Simply create as many nodes in the nodes array as there are operands and define a type and visual parameters for it. Don't forget the comma to seperate each node in the array.

Future work

Features and issues on the todo-list are:

jvansomeren commented 4 years ago

I have not checked the code; trust that that is ok. Have only read the readme. I'm glad that we reached this milestone and propose to continue with the merge.