Skip to main content

Tool for analyzing quantum circuits

Project description

QBenchAnalyzer

A tool to generate a set of metrics for quantum circuits.

How to use it

You can find an example of usage in the demo.py file. In general, you only want to use the generate_metrics method. Here is a little example of how to use it.

from QBenchAnalyzer import generate_metrics
from qiskit import QuantumCircuit

circuit = "qaoa_vanilla_04.qasm"
qc = QuantumCircuit.from_qasm_file(circuit)
metrics = generate_metrics(qc)
print(metrics)

and, if you used the same circuit as the included in the repository, this will return the following set of metrics:

{'qubit_connections': {2: {0, 1, 3}, 3: {0, 1, 2}, 1: {0, 2, 3}, 0: {1, 2, 3}}, 'n_qubits': 4, 
 'n_2gates_x_qubit': {2: 6, 3: 6, 1: 6, 0: 6}, 'n_gates': 26, 'depth': 17, 'avg_2gates_x_qubit': 6.0, 
 'n_consecutive_2_gates': 3, 'n_2_gates': 12, 'n_2gates_critical_path': 10, 'entanglement_ratio': 0.46153846153846156, 
 'critical_depth': 0.8333333333333334, 'parallelism': 0.17647058823529407, 'program_communication': 1.0, 
 'entanglement_variance': 0.0}

Alternatively, you can generate predefined images using the method analyze_circuit_group_structural, which will generate an analysis of the metrics for a group of circuits. For doing so, you will need to implement a class that implements the ICircuitGenerator interface and use it to tell the method which circuits will you want to use. Here is an example of a functional script for calculating the metrics of the QFT circuits using the circuits between 5 and 10 qubits:

# Importing all the necessary stuff
from QBenchAnalyzer.circuit_generator import ICircuitGenerator
from QBenchAnalyzer import analyze_circuit_group_structural
from qiskit import QuantumCircuit

# Creating a class that could read all the circuits
class CircuitGenerator(ICircuitGenerator): # It is important that implement the ICircuitGenerator
    def __init__(self, name, file_name):
        super().__init__(name)           # Title of the image
        self.file_name = file_name       # File name of the circuits to read

    def generate_qiskit_circuit(self, number_of_qubits):
        # Calculate the path of the circuit with 'number_of_qubits' for reading
        file_path = f"./test/{self.file_name}_{number_of_qubits}.qasm" 
        # Return the circuit as a QuantumCircuit object
        return QuantumCircuit.from_qasm_file(file_path)

# Call this method to generate the graphic image of all metrics of one circuit group
analyze_circuit_group_structural(
    CircuitGenerator("Testing QFT", "qft"), # Instance of the circuit generator
    5,                                      # Smallest number of qubits of the circuit group to read
    10,                                     # Bigger number of qubits of the circuit group to read
    "example.jpg",                          # Name for the output image
    "./images/"                             # Path were the output should be store
)

Executing the example will output an image that will look like the following:

Example of the image generated

And the final working directory tree will look like:

.
├── images
│   ├── example.jpg
├── test
│   ├── qft_5.qasm
│   ├── qft_6.qasm
│   ├── qft_7.qasm
│   ├── qft_8.qasm
│   ├── qft_9.qasm
│   └── qft_10.qasm
├── main.py

Also, if you are using statical predefined circuits, you can use the class StaticCircuitGenerator to simplify, as shown in the following example:

# Importing all the necessary stuff
from QBenchAnalyzer.circuit_generator import StaticCircuitGenerator
from QBenchAnalyzer import analyze_circuit_group_structural
    
# Create the instance of the StaticCircuitGenerator
circuit_generator = StaticCircuitGenerator(name='Testing QFT', path="./test/", circuit_name="qft")

# Call this method to generate the graphic image of all metrics of one circuit group
analyze_circuit_group_structural(
    circuit_generator,                 # Instance of the circuit generator
    5,                                 # Smallest number of qubits of the circuit group to read
    10,                                # Bigger number of qubits of the circuit group to read
    "example.jpg",                     # Name for the output image
    "./images/"                        # Path were the output should be store
)

Contributing

If you want to make changes, it is needed to test all the changes in local before a merge request. To do it, it is needed to install the local changes first, and then run the test. You can use the following commands as a guide:

pip install .
python3 -m pytest

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

qbenchanalyzer-0.0.4.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qbenchanalyzer-0.0.4-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file qbenchanalyzer-0.0.4.tar.gz.

File metadata

  • Download URL: qbenchanalyzer-0.0.4.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for qbenchanalyzer-0.0.4.tar.gz
Algorithm Hash digest
SHA256 467048a4569c8aeab100119db0c686612603d3292b0076c95f5295627c9a28bb
MD5 e0f7b6a2600878bd6214e9c1c2e1e2a6
BLAKE2b-256 efe9a2c73b3bede01d62a872345f63bbacef86b9728d9dfa6f56a143bc6cb35a

See more details on using hashes here.

File details

Details for the file qbenchanalyzer-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: qbenchanalyzer-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for qbenchanalyzer-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2b1315ff723e27a57663c343955175b985cf88478d62d8ad0cad3431e399f7d2
MD5 9b37246808c71dbf5c08088d939b03e8
BLAKE2b-256 92b3c3817156586b84819f60e14420077e685d4704c7b4d690db333b0e3adb06

See more details on using hashes here.

Supported by

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