Skip to content

Qedma CUDA-Q backend

Error mitigation with cudaq.observe() with QESEM. Jobs run on your QPU provider. Quantinuum is supported in this release (for example the H2-1 system).

Install

Needs CUDA-Q 0.16.0 or newer (import cudaq).

pip install 'cudaq>=0.16.0'
pip install qedma-cudaq

Optional — make the target visible to nvq++:

python -m qedma_cudaq --install-nvqpp

API token

You need an API token to authenticate with QESEM. Qedma Support provides this token when your QESEM account is created. See the installation guide for details.

Set the token as an environment variable

We recommend storing the token in the QEDMA_API_TOKEN environment variable. Export it in the same shell that runs your program:

export QEDMA_API_TOKEN="<your QESEM API token>"

You can then configure the Qedma target without passing the token explicitly:

cudaq.set_target("qedma")

Pass the token directly

Alternatively, you can pass the same token using the api_key argument:

cudaq.set_target("qedma", api_key="<your QESEM API token>")

api_key and QEDMA_API_TOKEN use the same API token. They are not separate credentials. If you provide both, the value passed through api_key takes precedence.

QPU token

QESEM submits the job to your provider account (for example Quantinuum). Register that vendor token once.

from qedma_cudaq import register_qpu_token, unregister_qpu_token

register_qpu_token(qpu_token="<provider token>")

QEDMA_QPU_TOKEN is the environment variable for the same vendor token. When it is set, register_qpu_token() with no arguments reads it.

Later observe calls do not take the token again. To replace it, call unregister_qpu_token(), then register_qpu_token(...) with the new token.

register_qpu_token / unregister_qpu_token authenticate with the API token from set_target(..., api_key=...) or from QEDMA_API_TOKEN.

Job flow

pip install qedma-cudaq registers the qedma target with CUDA-Q. import cudaq loads that plugin, so cudaq.set_target("qedma") finds the target, cudaq.observe() accepts the QESEM arguments below, and the result has the fields in Results. qedma_cudaq.register() loads the same plugin if you call it yourself.

Each cudaq.observe() call runs one QESEM step and waits until that step finishes. Resource estimation and the mitigation run execute on the QPU and can take a long time. cudaq.observe_async() is not supported on the qedma target.

  1. Create (the default, action=JobAction.CREATE_JOB). Wait until the analytical HQC estimate is ready. result.analytical_hqc_estimation is the estimated HQC required for resource estimation. result.expectation() is 0 here; the number you need is the estimate. Read result.job_id and store that string before you continue. Steps 2 and 3 pass it as job_id=. Keep the stored id so a later process can resume this job after a failure, instead of creating a new one.
  2. Resource estimation (action=JobAction.START_RESOURCE_ESTIMATION, with the job_id stored from step 1). Wait until the empirical HQC estimate is ready. result.empirical_hqc_estimation is the estimated HQC required for full mitigation. This step runs on the QPU.
  3. Start (action=JobAction.START_JOB, with the job_id stored from step 1 and max_hqc=). max_hqc is the cap you set for the mitigation run. Set it above empirical_hqc_estimation from step 2. The example uses 1.2 times that estimate, so a small overrun does not stop the run. result.expectation() is the mitigated expectation value. This step runs on the QPU.

If the requested precision is reached during resource estimation, no mitigation run is required. In that case result.status is JobStatus.SUCCEEDED and result.expectation() is already the mitigated value. Do not call JobAction.START_JOB for this job.

If mitigation is required, status is not JobStatus.SUCCEEDED until start finishes.

Example

Quantinuum H2-1. The same three calls apply to any other qedma_cudaq.Backend member. One observe() call takes one spin operator (a single Pauli term or a weighted sum). That operator is one observable.

import cudaq
from qedma_cudaq import Backend, JobAction, JobStatus, Provider, register_qpu_token

cudaq.set_target("qedma")  # or api_key="<your QESEM API token>"
register_qpu_token()  # or qpu_token="<provider token>"


@cudaq.kernel
def four_qubit():
    q = cudaq.qvector(4)
    h(q[0])
    x.ctrl(q[0], q[1])
    x.ctrl(q[1], q[2])
    x.ctrl(q[2], q[3])


H = 0.25 * (
    cudaq.spin.z(0) + cudaq.spin.z(1) + cudaq.spin.z(2) + cudaq.spin.z(3)
)

created = cudaq.observe(
    four_qubit,
    H,
    backend=Backend.H2_1,
    provider=Provider.QUANTINUUM,
    precision=0.1,
    description="four-qubit demo",
    enable_notifications=False,
)
job_id = created.job_id
# Save this string (for example write it to a file) so a new process can
# resume the job after a failure.

estimated = cudaq.observe(
    four_qubit,
    H,
    action=JobAction.START_RESOURCE_ESTIMATION,
    job_id=job_id,
)
if estimated.status == JobStatus.SUCCEEDED:
    result = estimated
else:
    # Cap for the mitigation run. 1.2 times the estimate leaves room if the
    # real HQC runs a bit over the forecast.
    max_hqc = 1.2 * estimated.empirical_hqc_estimation
    result = cudaq.observe(
        four_qubit,
        H,
        action=JobAction.START_JOB,
        job_id=job_id,
        max_hqc=max_hqc,
    )

print(result.expectation())

observe() prints the job log lines when it returns (see Results). description, enable_notifications, backend, provider, precision, and transpilation_level are sent only when creating the job. Resource estimation and start only need the stored job_id (and max_hqc on start).

Observe arguments

Keyword arguments of cudaq.observe. They are not set_target arguments.

Argument When Notes
backend create qedma_cudaq.Backend member, or its string (for example H2-1).
provider create qedma_cudaq.Provider.QUANTINUUM (or "quantinuum").
precision create Target absolute precision for the observable.
description create Label shown with the job in the Qedma dashboard.
enable_notifications create True (default) or False. When True, QESEM emails job status to the address on the account that owns the API token.
transpilation_level create TranspilationLevel.STANDARD (default). MINIMAL_WITH_LAYOUT_OPT keeps the circuit structure and optimizes the qubit layout. MINIMAL keeps the circuit structure without layout optimization.
action any qedma_cudaq.JobAction member: CREATE_JOB (default), START_RESOURCE_ESTIMATION, or START_JOB.
job_id resource estimation, start Required. The id string from create (result.job_id). Store it so a later process can resume the job.
max_hqc start Required. Max HQC you set for the mitigation run (action=JobAction.START_JOB).

Results

result.expectation() is the mitigated expectation value of the spin operator passed to observe, once QESEM has one. After create, and after resource estimation when the precision is not met yet, that number is 0. Use analytical_hqc_estimation after create and empirical_hqc_estimation after resource estimation for the next budget. When resource estimation already meets the precision, result.status is JobStatus.SUCCEEDED and expectation() is the mitigated value. result.mitigated stays None on that path. The same objects are in result.counts().annotations["empirical_estimation_mitigation_results"] under mitigated and unmitigated.

result.status is the QESEM job status, a qedma_cudaq.JobStatus member. These properties read the job fields on the result:

Property Meaning
job_id QESEM job id
warnings Messages from QESEM, as a list of strings (empty when none)
analytical_hqc_estimation Estimated HQC required for resource estimation, or None
empirical_hqc_estimation Estimated HQC required for full mitigation, or None
mitigated After start, mitigated expectation as {"value", "error_bar"}, or None
unmitigated After start, noisy expectation as {"value", "error_bar"}, or None

The spin operator is printed in the final-results log line. It is not a field on the result.

The same job fields are in result.counts().annotations. Call counts() with no arguments; counts(term) drops these fields.

Field Meaning
job_id QESEM job id
status QESEM status, for example SUCCEEDED
mitigated After start, mitigated expectation as {"value", "error_bar"}
unmitigated After start, noisy expectation as {"value", "error_bar"}, when QESEM sends it
empirical_estimation_mitigation_results When resource estimation already meets the precision, {"mitigated", "unmitigated"} with the same {"value", "error_bar"} objects
analytical_hqc_estimation Estimated HQC required for resource estimation
empirical_hqc_estimation Estimated HQC required for full mitigation
empirical_hqc HQC used by resource estimation
hqc Total HQC used by the job, including resource estimation and the mitigation run
max_hqc Max HQC defined by the user for the mitigation run (action=JobAction.START_JOB)
qpu_name Backend name reported by QESEM
description Label from create
total_execution_time ISO-8601 duration of QPU time plus classical processing
warnings Messages from QESEM, when present

hqc on a finished job is the total used, so it includes empirical_hqc. total_execution_time is the recorded duration, not a placeholder.

observe() prints the same job log lines as the qedma-api client when it returns. QESEM warnings are printed before the last line.

After create:

output
Submitting new job
[0123456789abcdef0123456789abcdef] New job created
[0123456789abcdef0123456789abcdef] Analytical resource estimation: [40.2]

After resource estimation:

output
[0123456789abcdef0123456789abcdef] Starting empirical resource estimation
[0123456789abcdef0123456789abcdef] Empirical HQC estimation: [610.0]

When resource estimation already reached the precision:

output
[0123456789abcdef0123456789abcdef] Starting empirical resource estimation
[0123456789abcdef0123456789abcdef] Required precision achieved during resource estimation
[0123456789abcdef0123456789abcdef] Empirical HQC estimation: [None]

After start:

output
[0123456789abcdef0123456789abcdef] Starting job
[0123456789abcdef0123456789abcdef] Final results: [{    {'Z0': 0.25, 'Z1': 0.25, 'Z2': 0.25, 'Z3': 0.25}: mitigated=ExpectationValue(value=0.608149721651, error_bar=0.0874262868029) unmitigated=ExpectationValue(value=0.41, error_bar=0.15)}]

Troubleshooting

Symptom Likely cause
Target qedma not found qedma-cudaq is not installed, or this process imported CUDA-Q before the plugin was available. Install the package and start a new process. qedma_cudaq.register() loads the plugin in the current process
Auth errors / "missing API token" Set the QEDMA_API_TOKEN environment variable, or pass that token as api_key to set_target. api_key wins if both are set
No registered QPU tokens Call qedma_cudaq.register_qpu_token(qpu_token=...), or set QEDMA_QPU_TOKEN and call register_qpu_token(). Call unregister_qpu_token() before registering a different token
observe_async raises On the qedma target call cudaq.observe(). It waits until that step finishes
missing precision / missing backend / missing provider Create needs provider=, precision=, and backend=
missing job_id Resource estimation and start need job_id= with the id string from create (result.job_id)
missing max_hqc Start needs max_hqc= (your cap for the mitigation run)
Start fails because the job already finished Resource estimation already finished the job (result.status is JobStatus.SUCCEEDED). Use that result and do not start it