Skip to content

Commit

Permalink
some code updates
Browse files Browse the repository at this point in the history
  • Loading branch information
beckykd committed Feb 8, 2024
1 parent 80673da commit 6031bcc
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 8 deletions.
18 changes: 17 additions & 1 deletion docs/build/bit-ordering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,27 @@ the value $2^\text{label}$ (label being the qubit's index in
with bit $0$ being `0`, and bit $1$ being `1`. This is interpreted as the
decimal integer `2` (measured with probability `1.0`).

```python
<Tabs>
<TabItem value="SamplerV2" label="SamplerV2">
```python
from qiskit.primitives import Samplerv2 as Sampler
qc.measure_all()

job = sampler.run([(qc)])
result = job.result()
print(f" > Counts: {result[0].data.evs}")
```
</TabItem>

<TabItem value="SamplerV1" label="Sampler (V1)">
```python
from qiskit.primitives import Sampler
qc.measure_all()

Sampler().run(qc).result().quasi_dists[0]
```
</TabItem>
</Tabs>

```
{2: 1.0}
Expand Down
18 changes: 17 additions & 1 deletion docs/run/configure-runtime-compilation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,21 @@ psi1_H1 = job.result()

### Example: configure Sampler with optimization levels

```python
<Tabs>
<TabItem value="SamplerV2" label="SamplerV2">
```python
from qiskit_ibm_runtime import QiskitRuntimeService, Samplerv2 as Sampler, Options

service = QiskitRuntimeService()
backend = service.backend("ibmq_qasm_simulator")
options = Options(optimization_level=1)

sampler = Sampler(options=options, backend=backend)
```
</TabItem>

<TabItem value="SamplerV1" label="Sampler (V1)">
```python
from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options

service = QiskitRuntimeService()
Expand All @@ -106,6 +120,8 @@ options = Options(optimization_level=1)

sampler = Sampler(options=options, backend=backend)
```
</TabItem>
</Tabs>


<span id="transpilation-table"></span>
Expand Down
40 changes: 39 additions & 1 deletion docs/run/estimate-job-run-time.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,43 @@ Quantum time is the duration, in seconds, a quantum system is committed to fulfi

Example:

```python
<Tabs>
<TabItem value="SamplerV2" label="SamplerV2">
```python
from qiskit import QuantumCircuit
from qiskit_ibm_runtime import QiskitRuntimeService, Samplerv2 as Sampler

service = QiskitRuntimeService()

# Create a new circuit with two qubits (first argument) and two classical
# bits (second argument)
qc = QuantumCircuit(2, 2)

# Add a Hadamard gate to qubit 0
qc.h(0)

# Perform a controlled-X gate on qubit 1, controlled by qubit 0
qc.cx(0, 1)

# Measure qubit 0 to cbit 0, and qubit 1 to cbit 1
qc.measure(0, 0)
qc.measure(1, 1)

# Run on the least-busy system you have access to
backend = service.least_busy(simulator=False,operational=True)

# Create a Sampler object
sampler = Sampler(backend)

# Submit the circuit to the sampler
job = sampler.run([(qc)])

print(job.usage_estimation)
```
</TabItem>

<TabItem value="SamplerV1" label="Sampler (V1)">
```python
from qiskit import QuantumCircuit
from qiskit_ibm_runtime import QiskitRuntimeService, Sampler

Expand Down Expand Up @@ -48,6 +84,8 @@ job = sampler.run(qc)

print(job.usage_estimation)
```
</TabItem>
</Tabs>

Output:

Expand Down
25 changes: 24 additions & 1 deletion docs/run/instances.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,28 @@ You can specify an instance when initializing the service or provider, or when c

### qiskit-ibm-runtime

```python
<Tabs>
<TabItem value="SamplerV2" label="SamplerV2">
```python

# Optional: List all the instances you can access.
service = QiskitRuntimeService(channel='ibm_quantum')
print(service.instances())

# Optional: Specify it at service level. This becomes the default unless overwritten.
service = QiskitRuntimeService(channel='ibm_quantum', instance="hub1/group1/project1")
backend1 = service.backend("ibmq_manila")

# Optional: Specify it at the backend level, which overwrites the service-level specification when this backend is used.
backend2 = service.backend("ibmq_manila", instance="hub2/group2/project2")

sampler1 = Samplerv2(backend=backend1) # this will use hub1/group1/project1
sampler2 = Samplerv2(backend=backend2) # this will use hub2/group2/project2
```
</TabItem>

<TabItem value="SamplerV1" label="Sampler (V1)">
```python

# Optional: List all the instances you can access.
service = QiskitRuntimeService(channel='ibm_quantum')
Expand All @@ -70,6 +91,8 @@ backend2 = service.backend("ibmq_manila", instance="hub2/group2/project2")
sampler1 = Sampler(backend=backend1) # this will use hub1/group1/project1
sampler2 = Sampler(backend=backend2) # this will use hub2/group2/project2
```
</TabItem>
</Tabs>

### qiskit-ibm-provider

Expand Down
56 changes: 55 additions & 1 deletion docs/run/quantum-serverless.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,58 @@ For each `IBMQPUSelector`, the context is set in the constructor. All `IBMQPUSel

Then, in step 3 of the pattern, you execute the target circuit on the system chosen by the selector. Since you optimized your circuit for the system in step 2, you can skip transpilation in the primitives by setting `skip_transpilation=True`.

```python
<Tabs>
<TabItem value="SamplerV2" label="SamplerV2">
```python
# source_files/my_qiskit_pattern_resource_management.py

from qiskit_ibm_runtime import QiskitRuntimeService, Session, Samplerv2 as Sampler, Options
from qiskit.circuit.random import random_circuit
from quantum_serverless_tools.selectors import IBMLeastNoisyQPUSelector

service = QiskitRuntimeService()

# Step 1: Map quantum circuits and operators
abstract_circuit = random_circuit(
num_qubits=5, depth=4, measure=True, seed=1234
)

# Step 2: Optimize the circuit for quantum execution with automatically selected system
selector = IBMLeastNoisyQPUSelector(
service, circuit=abstract_circuit, transpile_options={"optimization_level": 3}
)
backend = selector.get_backend(min_num_qubits=127)
target_circuit = selector.optimized_circuit

## Alternatively, one can automatically select a system according to most available:
# from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
# from quantum_serverless_tools.selectors import IBMLeastBusyQPUSelector
#
# backend = IBMLeastBusyQPUSelector(service).get_backend(min_num_qubits=127)
# pm = generate_preset_pass_manager(optimization_level=3, backend=backend)
# target_circuit = pm.run(abstract_circuit)

# Step 3: Execute the target circuit
with Session(service, backend=backend) as session:
sampler = Sampler(
options=Options(
execution={"shots": 1024}, transpilation={"skip_transpilation": True}
)
)
job = sampler.run([(target_circuit)])
result = job.result()

# Step 4: Postprocess the results
print(f" > Counts: {result[0].data.evs}")

# save results of program execution
# note: saved items must be serializable
save_result(result)
```
</TabItem>

<TabItem value="SamplerV1" label="Sampler (V1)">
```python
# source_files/my_qiskit_pattern_resource_management.py

from qiskit_ibm_runtime import QiskitRuntimeService, Session, Sampler, Options
Expand Down Expand Up @@ -166,4 +217,7 @@ print(result)
save_result(result)
```

</TabItem>
</Tabs>

After creating this pattern, you can deploy and run it remotely with Quantum Serverless as described above.
56 changes: 53 additions & 3 deletions docs/run/run-jobs-in-session.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,49 @@ class. The session starts when its first job begins execution.

**Session class**

``` python
<Tabs>
<TabItem value="PrimV2" label="V2 primitives">
``` python
from qiskit_ibm_runtime import Session, Samplerv2 as Sampler, Estimatorv2 as Estimator

session = Session(service=service, backend="ibmq_qasm_simulator")
estimator = Estimator(session=session)
sampler = Sampler(session=session)
```
</TabItem>

<TabItem value="PrimV1" label="V1 primitives">
``` python
from qiskit_ibm_runtime import Session, Sampler, Estimator

session = Session(service=service, backend="ibmq_qasm_simulator")
estimator = Estimator(session=session)
sampler = Sampler(session=session)
```
</TabItem>
</Tabs>

**Context manager**

The context manager automatically opens and closes the session.

``` python
<Tabs>
<TabItem value="PrimV2" label="V2 primitives">
``` python
with Session(service=service, backend="ibmq_qasm_simulator"):
estimatorv2 = Estimatorv2()
samplerv2 = Samplerv2()
```
</TabItem>

<TabItem value="PrimV1" label="V1 primitves">
``` python
with Session(service=service, backend="ibmq_qasm_simulator"):
estimator = Estimator()
sampler = Sampler()
```
</TabItem>
</Tabs>

When you start a session, you must specify a system or simulator. This can be done by specifying its name or by passing a `backend` object.

Expand Down Expand Up @@ -132,7 +158,28 @@ with Session(service=service, backend=backend) as session:

A session can handle multiple primitives, allowing for various operations within a single session. The following example shows how you can create both an instance of the `Sampler` class and one of the `Estimator` class and invoke their `run()` methods within a session.

```python
<Tabs>
<TabItem value="PrimV2" label="V2 primitives">
```python
from qiskit_ibm_runtime import Session, Samplerv2 as Sampler, Estimatorv2 as Estimator

with Session(backend=backend):
sampler = Sampler()
estimator = Estimator()

job = sampler.run([(sampler_circuit)])
result = job.result()
print(f" > Counts: {result[0].data.evs}")

job = estimator.run([(circuit, observable)])
result = job.result()
print(f" > Expectation value: {result[0].data.evs}")
print(f" > Metadata: {result[0].metadata}")
```
</TabItem>

<TabItem value="PrimV1" label="V1 primitives">
```python
from qiskit_ibm_runtime import Session, Sampler, Estimator

with Session(backend=backend):
Expand All @@ -145,6 +192,9 @@ with Session(backend=backend):
result = estimator.run(circuit, observable).result()
print(f">>> Expectation value from the estimator job: {result.values[0]}")
```
</TabItem>
</Tabs>

<span id="session-status"></span>
## Check session status

Expand Down

0 comments on commit 6031bcc

Please sign in to comment.