Skip to main content

quantum-viz.js Python tools

Project description

quantum-viz

quantum-viz is the Python package companion of quantum-viz.js, a JavaScript package that supports visualizing any arbitrary quantum gate, classical control logic and collapsed grouped blocks of gates using JSON-formatted input data. quantum-viz contains a Jupyter widget and will also include support for translating quantum circuits written in common quantum programming libraries to JSON using the quantum-viz.js JSON schema.

quantum-viz screenshot

Installation

You can install the quantum-viz.js widget via pip from PyPI:

pip install quantum-viz

Example

To use the quantum-viz widget, run the below example code in a Jupyter notebook cell:

from quantum_viz import Viewer

# Create a quantum circuit that prepares a Bell state
circuit = {
    "qubits": [{ "id": 0 }, { "id": 1, "numChildren": 1 }],
    "operations": [
        {
            "gate": 'H',
            "targets": [{ "qId": 0 }],
        },
        {
            "gate": 'X',
            "isControlled": "True",
            "controls": [{ "qId": 0 }],
            "targets": [{ "qId": 1 }],
        },
        {
            "gate": 'Measure',
            "isMeasurement": "True",
            "controls": [{ "qId": 1 }],
            "targets": [{ "type": 1, "qId": 1, "cId": 0 }],
        },
    ],
}

widget = Viewer(circuit)
widget # Display the widget

quantum-viz example

Qiskit Integration

By installing the optional [qiskit] dependency, you can leverage Qiskit's QuantumCircuit APIs to define the circuit and render it using the Viewer widget on Jupyter, for example:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from quantum_viz import Viewer

qr = QuantumRegister(3, 'q')
anc = QuantumRegister(1, 'ancilla')
cr = ClassicalRegister(3, 'c')
qc = QuantumCircuit(qr, anc, cr)


qc.h(qr[0:3])
qc.x(anc[0])
qc.h(anc[0])
qc.cx(qr[0:3], anc[0])
qc.h(qr[0:3])
qc.barrier(qr)
qc.measure(qr, cr)

Viewer(qc)

Optionally, you can also import the display method from quantum_viz.utils to render the circuit on a new browser window:

from quantum_viz.utils import display
display(qc)

Contributing

Check out our contributing guidelines to find out how you can contribute to quantum-viz.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quantum-viz-1.0.5.tar.gz (10.1 kB view hashes)

Uploaded Source

Built Distribution

quantum_viz-1.0.5-py3-none-any.whl (10.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page