Skip to main contentIBM Quantum Documentation

Get started with primitives

The steps in this topic describes how to set up primitives, explore the options you can use to configure them, and invoke them in a program.

Important

To ensure faster and more efficient results, as of 1 March 2024, circuits and observables need to be transformed to only use instructions supported by the system (referred to as instruction set architecture (ISA) circuits and observables) before being submitted to the Qiskit Runtime primitives. See the transpilation documentation for instructions to transform circuits. Due to this change, the primitives will no longer perform layout or routing operations; consequently, transpilation options referring to those tasks will no longer have any effect. Users can still request that the primitives do no optimization of input circuits by specifying options.transpilation.skip_transpilation.

Note

These examples all use the primitives from Qiskit® Runtime, but you could use the base primitives instead.


Get started with Estimator

1. Initialize the account

Since Qiskit Runtime Estimator is a managed service, you will first need to initialize your account. You can then select the simulator or real system you want to use to calculate the expectation value.

Follow the steps in the Install and set up topic if you don't already have an account.

from qiskit_ibm_runtime import QiskitRuntimeService
 
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")

2. Create a circuit and an observable

Just like the section before, you will need at least one circuit and one observable as inputs to the Estimator primitive.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp, random_hermitian
 
n_qubits = 127
 
mat = np.real(random_hermitian(n_qubits, seed=1234))
circuit = IQP(mat)
observable = SparsePauliOp("Z" * n_qubits)
print(f">>> Observable: {observable.paulis}")

The circuit and observable need to be transformed to only use instructions supported by the system. We'll use the transpiler to do this.

pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)
isa_observable = observable.apply_layout(isa_circuit.layout)

3. Initialize the Qiskit Runtime Estimator

Here we are initializing an instance of qiskit_ibm_runtime.Estimator instead of qiskit.primitives.Estimator to use Qiskit Runtime's implementation of the Estimator.

When you initialize the Estimator, you'll need to pass in the system or simulator you previously selected as the target device (or simulator). You could also do this within the session parameter.

from qiskit_ibm_runtime import Estimator
 
estimator = Estimator(backend=backend)

4. Invoke the Estimator and get results

You can then invoke the run() method to calculate expectation values for the input circuits and observables.

job = estimator.run(isa_circuit, isa_observable)
print(f">>> Job ID: {job.job_id()}")
print(f">>> Job Status: {job.status()}")
result = job.result()
print(f">>> {result}")
print(f"  > Expectation value: {result.values[0]}")
print(f"  > Metadata: {result.metadata[0]}")

Get started with Sampler

1. Initialize the account

Since Qiskit Runtime Sampler is a managed service, you will first need to initialize your account. You can then select the simulator or real system you want to use to calculate the expectation value.

Follow the steps in the Install and set up topic if you don't already have an account set up.

from qiskit_ibm_runtime import QiskitRuntimeService
 
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")

2. Create a circuit

Just like the section before, you will need at least one circuit as the input to the Sampler primitive.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import random_hermitian
 
n_qubits = 127
 
mat = np.real(random_hermitian(n_qubits, seed=1234))
circuit = IQP(mat)
circuit.measure_all()

Again, we use the transpiler to get an ISA circuit.

pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)

3. Initialize the Qiskit Runtime Sampler

Here we are initializing an instance of qiskit_ibm_runtime.Sampler instead of qiskit.primitives.Sampler to use Qiskit Runtime's implementation of the Sampler.

When you initialize the Sampler, you'll need to pass in the simulator or system you previously selected as the target device (or simulator). You could also do this within the session parameter.

from qiskit_ibm_runtime import Sampler
 
sampler = Sampler(backend=backend)

4. Invoke the Sampler and get results

You can then invoke the run() method to generate a quasi-probability distribution for the input circuits and quantum states.

job = sampler.run(isa_circuit)
print(f">>> Job ID: {job.job_id()}")
print(f">>> Job Status: {job.status()}")
result = job.result()
print(f">>> {result}")
print(f"  > Quasi-probability distribution: {result.quasi_dists[0]}")
print(f"  > Metadata: {result.metadata[0]}")

Next steps

Recommendations
Was this page helpful?