# Connect your IBM Quantum account

IBM Quantum devices run on your own IBM Quantum account: this page shows how to connect it in the
router web app, what the router checks, and how your IBM jobs behave.

## How it works

IBM devices have IDs `ibm/<backend>`, for example `ibm/ibm_fez`. You connect your IBM Quantum
account once, and every IBM job then runs on that account.

The router charges nothing for IBM jobs, so your balance needs no funds to run them. IBM bills your
instance for the quantum time they use.

## Connect your account

You connect your account in the router web app only.

1. Sign in at https://router.eigenorbit.com and accept the router terms. Connecting needs them.
2. Open the IBM account page, https://router.eigenorbit.com/ibm.
3. Paste the API token and the instance CRN from your IBM Quantum Platform dashboard. The CRN
   starts with `crn:`.
4. Choose **Connect IBM account**.

## What the router checks

The router checks the token and the instance with IBM before it saves anything, and answers within
20 seconds:

- IBM accepts the token.
- IBM finds an instance with that CRN that the token can use.
- The instance can use at least one IBM device.

A failed check saves nothing and leaves an existing connection as it was. Once saved, the page
shows the last four characters of the token, the instance's name, plan and region, when it was last
checked with IBM, and the IBM devices the instance could use at that check.

The token is stored encrypted and used only to submit, check and cancel your jobs on that instance.
The router never shows the full token or the CRN again.

## When connecting fails

A failed check saves nothing. The codes behind it, and what the web app shows for each:

| Code | What happened | What the web app shows |
|---|---|---|
| `ibm_token_rejected` | IBM did not accept the token. | "IBM didn't accept this token. Check that you copied the whole API key and try again." |
| `ibm_instance_not_found` | IBM found no instance with that CRN that the token can use. | "IBM couldn't find this instance for that token. Check the instance CRN, and that the token can use it." |
| `ibm_no_devices` | The instance can use no IBM device. | "This instance has no devices you can use. Choose an instance that can run on at least one IBM device." |
| `ibm_unreachable` | IBM did not answer the check, or answered with a 429 or 5xx status. | "Couldn't reach IBM to check these. Try again in a moment." |
| `ibm_validation_unavailable` | The router could not finish the check within 20 seconds. | The same message as `ibm_unreachable`. |
| `ibm_connection_busy` | Another save or disconnect of your connection is in progress, or you saved more than 10 times in 10 minutes. | "Couldn't save your IBM account. Try again." Wait until the other change finishes, or try again later. |
| `terms_not_accepted` | You have not accepted the current router terms. | The router terms screen. Accept the terms, then connect. |
| `validation_error` | The CRN is malformed, or the token is shorter than 8 or longer than 512 characters. | "Couldn't save your IBM account. Try again." Check what you pasted. |

## Run a job on an IBM device

Use an IBM device ID as `device`. IBM devices run `run`, `sample` and `probabilities`, and have no
availability windows (see [Devices and prices](/docs/devices)).

You can pass Qiskit Runtime options to the job's primitive: in eigenkit as `primitive_options`, with
the name and shape of `primitive_options` on eigenkit's `QiskitRuntimeConfig`; over HTTP as
`ibm_options` in the job body.

<CodeTabs syncKey="client">

```python title="eigenkit"
from eigenorbit_router_client import RouterConfig

provider = RouterConfig(
    device="ibm/ibm_fez",
    shots=4000,
    primitive_options={
        "dynamical_decoupling": {"enable": True, "sequence_type": "XpXm"},
        "twirling": {
            "enable_gates": True,
            "num_randomizations": 40,
            "shots_per_randomization": 100,
        },
    },
)
```

```bash title="HTTP"
curl https://router.eigenorbit.com/v1/jobs \
  --request POST \
  --header 'Content-Type: application/json' \
  --header 'EO-Protocol-Version: 1' \
  --header "Idempotency-Key: $(uuidgen)" \
  --header "Authorization: Bearer $EIGENORBIT_ROUTER_API_KEY" \
  --data '{
    "device": "ibm/ibm_fez",
    "method": "run",
    "shots": 4000,
    "circuit_spec": {
      "n_qubits": 1,
      "n_weights": 1,
      "n_inputs": 0,
      "gates": [{"n": "RY", "q": [0], "p": [{"t": "WeightRef", "i": 0}]}]
    },
    "observables": [{"terms": [{"coeff": 1.0, "paulis": [["Z", 0]]}]}],
    "weights": [0.5],
    "inputs": [],
    "ibm_options": {
      "dynamical_decoupling": {"enable": true, "sequence_type": "XpXm"},
      "twirling": {
        "enable_gates": true,
        "num_randomizations": 40,
        "shots_per_randomization": 100
      }
    }
  }'
```

</CodeTabs>

Use the eigenkit `provider` as in the [Quickstart](/docs/quickstart). The options the router accepts:

- `dynamical_decoupling`: `enable`, and `sequence_type` `XX`, `XpXm` or `XY4`.
- `twirling`: `enable_gates`, `enable_measure`, `num_randomizations` and
  `shots_per_randomization`. With twirling, `num_randomizations` times `shots_per_randomization`
  must equal `shots`: here 40 times 100 is 4000.

Anything else is refused with `422 validation_error` before the job is created. Only IBM devices
take these options: on any other device eigenkit's `RouterConfig` raises `ValueError`, and the
router answers `422 validation_error`.

## What an IBM job shows

- `credential_source` is `own`: the job ran on the IBM Quantum account you connected. It is null
  for an Amazon Braket job.
- `ibm_usage_seconds` is the IBM quantum time the job used, in seconds with millisecond precision.
  It is null until IBM reports it, and can stay null if IBM never does.
- `billing` is null and `estimate_usd_micro` is 0: the job carries no router charge (see
  [Balance, holds and charges](/docs/billing)).
- The result's `execution` names the IBM job ID and what IBM ran: the physical qubits, the
  transpiled circuit and the transpiler settings (see [Job lifecycle](/docs/jobs)).

## Errors on IBM jobs

Before a job is created, a submit to an IBM device can answer:

| Code | What happened | What to do |
|---|---|---|
| `403 ibm_account_not_connected` | You have no IBM Quantum account connected. `details = {device}`. | Connect your account on the IBM account page. |
| `403 ibm_connection_invalid` | Your connection stopped working: IBM refused its token or instance while a job ran. | Reconnect on the IBM account page. |

No job is created for either, and an estimate reports both in `would_reject_reason`.

A job that was accepted can fail with one of these job error codes, all of category `vendor`:

| Code | What happened |
|---|---|
| `ibm_credentials_rejected` | IBM refused your token or instance while the job ran. The connection becomes invalid, and later IBM jobs answer `ibm_connection_invalid` until you reconnect. |
| `ibm_quota_exhausted` | Your instance's quantum time is used up. |
| `ibm_target_mismatch` | IBM refused the compiled circuit because it no longer matches the device, for example after a recalibration. The router does not retry it: submit the job again. |
| `ibm_device_not_on_instance` | Your instance cannot use this device. Choose a device the IBM account page lists for your instance. |

Any other IBM failure is `vendor_rejected` or `vendor_failed`. A failed IBM job is still free.

## Replace or reconnect

To use another token or instance, choose **Replace connection** on the IBM account page. When the
connection stopped working, the button is **Reconnect IBM account**. The new token and instance are
checked with IBM first; only then is the old connection removed.

A successful replace first cancels your IBM jobs still queued or running on the old connection. IBM
may still finish a job that has already started.

## Disconnect

Choose **Disconnect** on the IBM account page. The router cancels your IBM jobs still queued or
running on your account, deletes the stored token and tells you how many jobs it cancelled. IBM jobs
are then refused with `ibm_account_not_connected` until you connect an account again.

A disconnect can also answer `ibm_connection_busy`, or `ibm_validation_unavailable` when the router
could not finish it within 20 seconds. Nothing is removed then; try again.

## Where IBM Quantum is not available

IBM Quantum is not available in the countries IBM restricts. There the device list leaves out IBM
devices, an IBM submit or estimate answers `451 region_blocked`, and connecting an account answers
`451 region_blocked`. The rest of the router stays available.

## Reference

- [Submit a job](/docs/api/jobs#submit-a-job)
- [Estimate a job](/docs/api/jobs#estimate-a-job)
- [Get a job](/docs/api/jobs#get-a-job)
- [Get device calibration](/docs/api/devices#get-device-calibration)
