Introduction to Qrisp and VTT QX

This notebook demonstrates how to use Qrisp, a high-level quantum programming language, with VTT’s quantum computers.

We’ll start by defining a custom function VTTBackend that will take care of job submissions to VTT QX.

import qiskit

from qrisp.interface import VirtualBackend


def VTTBackend(api_token, device_instance, port=None):
    """
    This function instantiates an IQMBackend based on VirtualBackend
    using Qiskit and Qiskit-on-IQM.


    Parameters
    ----------
    api_token : str
        An API token retrieved from VTT QX.
    device_instance : str
        The device instance of the IQM backend such as "q5" or "q50".
        For an up-to-date list, see the VTT QX dashboard.
    port : int, optional
        The port to listen. The default is None.
    """

    if not isinstance(api_token, str):
        raise TypeError(
            "api_token must be a string. You can create an API token using the VTT QX dashboard."
        )

    if not isinstance(device_instance, str):
        raise TypeError(
            "Please provide a device_instance as a string. You can see a list of available devices id in the VTT QX dashboard."
        )

    try:
        from iqm.qiskit_iqm import IQMProvider, transpile_to_IQM
    except ImportError:
        raise ImportError(
            "Please install qiskit-iqm to use the IQMBackend. You can do this by running `pip install qrisp[iqm]`."
        )

    def run_func_iqm(qasm_str, shots=None, token=""):
        if shots is None:
            shots = 1000
        server_url = "https://qx.vtt.fi/api/devices/" + device_instance
        backend = IQMProvider(server_url, token=api_token).get_backend()
        qc = qiskit.QuantumCircuit.from_qasm_str(qasm_str)
        qc_transpiled = transpile_to_IQM(qc, backend)

        job = backend.run(qc_transpiled, shots=shots)
        import re

        counts = job.result().get_counts()
        new_counts = {}
        for key in counts.keys():
            counts_string = re.sub(r"\W", "", key)
            new_counts[counts_string] = counts[key]

        return new_counts

    return VirtualBackend(run_func_iqm, port=port)

1. Setting Up the VTT Backend

The VTTBackend function connects Qrisp to VTT’s quantum computers through the VTT QX platform. It requires:

  • api_token: Your personal access token from your VTT QX project.
  • device_instance: The quantum device slug (q5 or q50).
qrisp_qx = VTTBackend(api_token="YOUR_API_TOKEN_HERE", device_instance="q50")

# Replace YOUR_API_TOKEN_HERE with your actual token from VTT QX.

2. Running a Simple Qrisp Program

from qrisp import QuantumVariable, h, cx


# Create a GHZ state with 3 qubits
def create_ghz_state(n_qubits=3):
    # Create a quantum variable with n_qubits
    qv = QuantumVariable(n_qubits)

    # Apply Hadamard to the first qubit
    h(qv[0])

    # Apply CNOTs to create entanglement
    for i in range(n_qubits - 1):
        cx(qv[i], qv[i + 1])

    return qv


# Create the GHZ state
ghz = create_ghz_state(3)

# Measure the state
result_ghz = ghz.get_measurement(backend=qrisp_qx, shots=1000)
print(result_ghz)

# For simulation, use the default backend
# result_ghz_sim = ghz.get_measurement(shots=1000)
# print("Simulated GHZ state measurement:")
# print(result_ghz_sim)
{'000': 0.463, '111': 0.41, '011': 0.04, '110': 0.036, '001': 0.018, '100': 0.014, '101': 0.013, '010': 0.006}

Conclusion

This tutorial demonstrated how to: - Connect Qrisp to VTT QX quantum computers - Create and execute a simple GHZ quantum circuits

For more information, check the Qrisp documentation and the VTT QX documentation.