VirtualBackend#

class VirtualBackend(run_func, name=None)[source]#

A Backend that wraps a user-provided circuit execution function.

This class replaces the legacy VirtualBackend (which depended on the now-removed BackendClient / BackendServer infrastructure). It follows the standard Backend interface and executes circuits synchronously by calling the user-supplied run_func for each circuit.

Deprecated since version 0.8: VirtualBackend is deprecated. New code should subclass Backend directly instead. See QrispSimulatorBackend for a reference implementation.

Parameters:
run_funccallable

A function with signature run_func(qasm_str, shots, token) that receives the circuit’s QASM string, an integer shot count (or None), and a token string. It must return a dict mapping bitstring outcomes to counts (or probabilities when shots is None).

namestr, optional

A human-readable name for this backend. Defaults to "VirtualBackend".

Examples

We set up a VirtualBackend that prints the received circuit and returns the results of the QASM simulator. It is recommended that run_func provides a default value of None for the shots parameter, substituting its own default:

def run_func(qasm_str, shots=None, token=""):
    if shots is None:
        shots = 1000

    from qiskit import QuantumCircuit

    qiskit_qc = QuantumCircuit.from_qasm_str(qasm_str)
    print(qiskit_qc)

    from qiskit_aer import AerSimulator
    qiskit_backend = AerSimulator()

    return qiskit_backend.run(qiskit_qc, shots=shots).result().get_counts()
>>> from qrisp.interface import VirtualBackend
>>> example_backend = VirtualBackend(run_func)
>>> from qrisp import QuantumFloat
>>> qf = QuantumFloat(3)
>>> qf[:] = 4
>>> qf.get_measurement(backend=example_backend)
             ┌─┐
qf.0:   ─────┤M├──────
             └╥┘┌─┐
qf.1:   ──────╫─┤M├───
        ┌───┐ ║ └╥┘┌─┐
qf.2:   ┤ X ├─╫──╫─┤M├
        └───┘ ║  ║ └╥┘
cb_0: 1/══════╩══╬══╬═
              0  ║  ║
cb_1: 1/═════════╩══╬═
                 0  ║
cb_2: 1/════════════╩═
{4: 1.0}