Skip to content

Commit

Permalink
custom image example doc updates (#1398)
Browse files Browse the repository at this point in the history
* custom image example doc updates
  • Loading branch information
akihikokuroda authored Jul 3, 2024
1 parent ff57279 commit 44ca9e9
Showing 1 changed file with 103 additions and 79 deletions.
182 changes: 103 additions & 79 deletions docs/deployment/deploying_custom_image_function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,51 +114,61 @@ Function example (runner.py)
.. code-block::
:caption: runner.py
from qiskit_aer import AerSimulator
from qiskit_serverless import get_arguments, save_result
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_aer import AerSimulator
from qiskit_serverless import get_arguments, save_result
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import Session
def custom_function(arguments):
def custom_function(arguments):
service = arguments.get("service")
circuit = arguments.get("circuit")
observable = arguments.get("observable")
backend = service.least_busy(operational=True, simulator=False, min_num_qubits=127)
session = Session(backend=backend)
target = backend.target
pm = generate_preset_pass_manager(target=target, optimization_level=3)
if service:
backend = service.least_busy(operational=True, simulator=False, min_num_qubits=127)
session = Session(backend=backend)
else:
backend = AerSimulator()
target_circuit = pm.run(circuit)
target_observable = observable.apply_layout(target_circuit.layout)
from qiskit_ibm_runtime import EstimatorV2 as Estimator
estimator = Estimator(session=session)
job = estimator.run([(target_circuit, target_observable)])
target = backend.target
pm = generate_preset_pass_manager(target=target, optimization_level=3)
session.close()
return job.result()[0].data.evs
target_circuit = pm.run(circuit)
target_observable = observable.apply_layout(target_circuit.layout)
class Runner:
def run(self, arguments: dict) -> dict:
return custom_function(arguments)
from qiskit_ibm_runtime import EstimatorV2 as Estimator
if service:
estimator = Estimator(session=session)
else:
estimator = Estimator(backend=backend)
job = estimator.run([(target_circuit, target_observable)])
if service:
session.close()
return job.result()[0].data.evs
class Runner:
def run(self, arguments: dict) -> dict:
return custom_function(arguments)
Dockerfile

.. code-block::
:caption: Dockerfile
FROM icr.io/quantum-public/qiskit-serverless-ray-node:0.12.0-py310
FROM icr.io/quantum-public/qiskit-serverless/ray-node:0.12.0-py310
# install all necessary dependencies for your custom image
# install all necessary dependencies for your custom image
# copy our function implementation in `/runner.py` of the docker image
USER 0
# copy our function implementation in `/runner.py` of the docker image
USER 0
RUN pip install qiskit_aer
WORKDIR /runner
COPY ./runner.py /runner
WORKDIR /
WORKDIR /runner
COPY ./runner.py /runner
WORKDIR /
USER $RAY_UID
USER $RAY_UID
Build container image

Expand All @@ -172,35 +182,38 @@ Build container image
The build container image need to be tagged and uploaded to the image registory that can be accessible from the gateway

Upload and register function


Set GATEWAY_TOKEN, GATEWAY_HOST, PROVIDER_ID and YOUR_TOKEN (YOUR_TOKEN is necessary when use_session=True only) environment variables.

.. code-block::
:caption: upload.py
import os
from qiskit_serverless import QiskitFunction, ServerlessClient
import os
from qiskit_serverless import QiskitFunction, ServerlessClient
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "<TOKEN>"),
host=os.environ.get("GATEWAY_HOST", "<GATEWAY ADDRESS>"),
)
help = """
title: custom-image-function
description: sample function implemented in a custom image
arguments:
service: service created with the accunt information
circuit: circuit
observable: observable
"""
function_with_custom_image = QiskitFunction(
title="custom-image-function",
image="<image retistory/image name:image tag>",
provider="<provider id>",
description=help
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
)
serverless.upload(function_with_custom_image)
help = """
title: custom-image-function
description: sample function implemented in a custom image
arguments:
service: service created with the accunt information
circuit: circuit
observable: observabl
"""
function_with_custom_image = QiskitFunction(
title="custom-image-function",
image="<image retistory/image name:image tag>",
provider=os.environ.get("PROVIDER_ID", "mockprovider"),
description=help
)
serverless.upload(function_with_custom_image)
For the User

Expand All @@ -209,46 +222,57 @@ List all available functions
.. code-block::
:caption: list.py
import os
from qiskit_serverless import ServerlessClient
import os
from qiskit_serverless import ServerlessClient
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "<TOKEN>"),
host=os.environ.get("GATEWAY_HOST", "<GATEWAY ADDRESS>"),
)
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
)
my_functions = serverless.list()
for function in my_functions:
print("Name: " + function.title)
print(function.description)
print()
my_functions = serverless.list()
for function in my_functions:
print("Name: " + function.title)
print(function.description)
print()
Execute Function

.. code-block::
:caption: usage.py
import os
from qiskit_serverless import ServerlessClient
from qiskit import QuantumCircuit
from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService("YOUR_TOKEN")
circuit = random_circuit(2, 2, seed=1234)
observable = SparsePauliOp("IY")
import os
from qiskit_serverless import ServerlessClient
from qiskit import QuantumCircuit
from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_ibm_runtime import QiskitRuntimeService
# set this True for the real Quantum system use
use_service=False
service = None
if use_service:
service = QiskitRuntimeService(
token=os.environ.get("YOUR_TOKEN", ""),
channel='ibm_quantum',
instance='ibm-q/open/main',
verify=False,
)
circuit = random_circuit(2, 2, seed=1234)
observable = SparsePauliOp("IY")
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
)
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "<TOKEN>"),
host=os.environ.get("GATEWAY_HOST", "<GATEWAY ADDRESS>"),
)
my_function = serverless.get("custom-image-function")
job = my_function.run(service=service, circuit=circuit, observable=observable)
my_function = serverless.get("custom-image-function")
job = my_function.run(service=service, circuit=circuit, observable=observable)
print(job.result())
print(job.logs())
print(job.result())
print(job.logs())
Expand Down

0 comments on commit 44ca9e9

Please sign in to comment.