Quickstart: Submit a circuit with a provider-specific format to Azure Quantum

Learn how to use the azure-quantum Python package to submit provider-specific formatted quantum circuits, (for example, OpenQASM 2.0 or IonQ JSON), to an IonQ or Quantinuum quantum computing target via the Azure Quantum service. For more information, see Quantum circuits.

Prerequisites

Load the required imports

First, run the following cell to load the required imports:

from azure.quantum import Workspace

Connect to the Azure Quantum service

To connect to the Azure Quantum service, your program will need the resource ID and the location of your Azure Quantum workspace. Log in to your Azure account, https://portal.azure.com, navigate to your Azure Quantum workspace, and copy the values from the header.

How to retrieve the resource ID and location from an Azure Quantum workspace

Paste the values into the following Workspace constructor to create a workspace object that connects to your Azure Quantum workspace.

workspace = Workspace(
    resource_id="",
    location=""
)

Submit a quantum circuit to IonQ

  1. Create a quantum circuit using the the language-agnostic JSON format supported by the IonQ targets, as described in the IonQ API documentation. For example, the following sample creates a superposition between three qubits:

    circuit = {
        "qubits": 3,
        "circuit": [
            {
            "gate": "h",
            "target": 0
            },
            {
            "gate": "cnot",
            "control": 0,
            "target": 1
            },
            {
            "gate": "cnot",
            "control": 0,
            "target": 2
            },
        ]
    }
    
  2. Submit the circuit to the IonQ target. The following example uses the IonQ simulator, which returns a Job object. For more information, see Azure Quantum Job.

    target = workspace.get_targets(name="ionq.simulator")
    job = target.submit(circuit)
    
  3. Wait until the job is complete and then fetch the results.

    results = job.get_results()
    print(results)
    
    .....
    {'duration': 8240356, 'histogram': {'0': 0.5, '7': 0.5}}
    
  4. You can then visualize the results using Matplotlib.

    import pylab as pl
    pl.rcParams["font.size"] = 16
    hist = {format(n, "03b"): 0 for n in range(8)}
    hist.update({format(int(k), "03b"): v for k, v in results["histogram"].items()})
    pl.bar(hist.keys(), hist.values())
    pl.ylabel("Probabilities")
    

IonQ job output

Estimate job cost

Before running a job on the QPU, you can estimate how much it will cost to run. To estimate the cost of running a job on the QPU,you can use the estimate_cost method:

target = workspace.get_targets(name="ionq.qpu")
cost = target.estimate_cost(circuit, num_shots=500)

print(f"Estimated cost: {cost.estimated_total}")

This prints the estimated cost in USD.

For the most current pricing details, see IonQ Pricing, or find your workspace and view pricing options in the "Provider" tab of your workspace via: aka.ms/aq/myworkspaces.

Load the required imports

First, run the following cell to load the required imports:

from azure.quantum import Workspace

Connect to the Azure Quantum service

To connect to the Azure Quantum service, your program will need the resource ID and the location of your Azure Quantum workspace. Log in to your Azure account, https://portal.azure.com, navigate to your Azure Quantum workspace, and copy the values from the header.

How to retrieve the resource ID and location from an Azure Quantum workspace

Paste the values into the following Workspace constructor to create a workspace object that connects to your Azure Quantum workspace.

workspace = Workspace(
    resource_id="",
    location=""
)

Submit a quantum circuit to the Quantinuum API validator

Note

The Quantinuum API validator target will always return 0 on measurement.

  1. Create a quantum circuit in the OpenQASM representation. For example, the following example creates a Teleportation circuit:

    circuit = """OPENQASM 2.0;
    include "qelib1.inc";
    qreg q[3];
    creg c0[3];
    h q[0];
    cx q[0], q[1];
    cx q[1], q[2];
    measure q[0] -> c0[0];
    measure q[1] -> c0[1];
    measure q[2] -> c0[2];
    """
    

    Optionally, you can load the circuit from a file:

    with open("my_teleport.qasm", "r") as f:
        circuit = f.read()
    
  2. Submit the circuit to the Quantinuum target. The following example uses the Quantinuum API validator, which returns a Job object. For more information, see Azure Quantum Job.

    target = workspace.get_targets(name="quantinuum.hqs-lt-s1-apival")
    job = target.submit(circuit, num_shots=500)
    
  3. Wait until the job is complete and then fetch the results.

    results = job.get_results()
    print(results)
    
    ........
    {'c0': ['000',
    '000',
    '000',
    '000',
    '000',
    '000',
    '000',
    ...
    ]}
    
  4. You can then visualize the results using Matplotlib.

    import pylab as pl
    pl.hist(results["c0"])
    pl.ylabel("Counts")
    pl.xlabel("Bitstring")
    

Quantinuum job output

Estimate job cost

Before running a job on the QPU, you can estimate how much it will cost to run. To estimate the cost of running a job on the QPU, you can use the estimate_cost method:

target = workspace.get_targets(name="quantinuum.hqs-lt-s1")
cost = target.estimate_cost(circuit, num_shots=500)

print(f"Estimated cost: {cost.estimated_total}")

This prints the estimated cost in H-System Quantum Credits (HQCs).

For the most current pricing details, see System Model H1, Powered by Honeywell, or find your workspace and view pricing options in the "Provider" tab of your workspace via: aka.ms/aq/myworkspaces.