VirtualBackend#
- class VirtualBackend(run_func, name=None)[source]#
A
Backendthat wraps a user-provided circuit execution function.This class replaces the legacy
VirtualBackend(which depended on the now-removedBackendClient/BackendServerinfrastructure). It follows the standardBackendinterface and executes circuits synchronously by calling the user-suppliedrun_funcfor each circuit.Deprecated since version 0.8:
VirtualBackendis deprecated. New code should subclassBackenddirectly instead. SeeQrispSimulatorBackendfor 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 (orNone), and a token string. It must return adictmapping bitstring outcomes to counts (or probabilities whenshotsisNone).- namestr, optional
A human-readable name for this backend. Defaults to
"VirtualBackend".
Examples
We set up a
VirtualBackendthat prints the received circuit and returns the results of the QASM simulator. It is recommended thatrun_funcprovides a default value ofNonefor theshotsparameter, 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}