quantum-serverless/docs/getting_started/experimental/file_download.ipynb

240 lines
5.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "aa077791-1bcf-43f6-ab2b-e04db48b9cb1",
"metadata": {},
"source": [
"# File download (Experimental)\n",
"\n",
"In this tutorial we will describe a way to retrieve files produced by Qiskit Functions.\n",
"\n",
"This function provides a way to download files produced by functions during execution. All you need is to call `QiskitServerless.file_download` function and pass a file name and the Qiskit Function to start downloading the file. Or you can list all available files to you by calling `QiskitServerless.files`.\n",
"\n",
"Limitations:\n",
"\n",
"- files should be saved in `/data` directory during your function execution to be visible by `.files()` method call.\n",
"- only `/data` directory is supported, `/data/other_folder` will not be visible.\n",
"- as a provider you have access to `/function-data`, it works in a similar way as the `/data` folder with the distinction that users don't have access to it. Only the providers of the specific functions can see files under that path.\n",
"- Qiskit Functions created by you and Qiskit Functions created by others don't share directories.\n",
"\n",
"> ⚠ This provider is set up with default credentials to a test cluster intended to run on your machine. For information on setting up infrastructure on your local machine, check out the guide on [local infrastructure setup](https://qiskit.github.io/qiskit-serverless/deployment/local.html)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "93717e14-d06e-4e11-bd5b-6cdc3f1b1abd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<gateway-client>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"from qiskit_serverless import ServerlessClient, QiskitFunction\n",
"\n",
"serverless = ServerlessClient(\n",
" token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n",
" host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost\"),\n",
" # If you are using the kubernetes approach the URL must be http://localhost\n",
")\n",
"serverless"
]
},
{
"cell_type": "markdown",
"id": "fc30a74a-2100-40b8-a283-30bd51875b45",
"metadata": {},
"source": [
"Let's create a Qiskit Function to write `tar` file into `/data` folder"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8d93f33b-f7f1-475d-b46e-1106cbe45cae",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"QiskitFunction(file-producer)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function = QiskitFunction(\n",
" title=\"file-producer\", entrypoint=\"produce_files.py\", working_dir=\"./source_files/\"\n",
")\n",
"\n",
"serverless.upload(function)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3577cc07",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"QiskitFunction(file-producer)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_function = serverless.get(\"file-producer\")\n",
"my_function"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3fef0868-7574-4fbf-b8de-4a7889bdf5ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Job | 4868d039-3342-4f3e-b843-c0d15ad52fe6>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"job = my_function.run()\n",
"job"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ecd0bb68-4d3c-450e-b363-a58fd91880b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Message': 'my_file.txt archived into my_file.tar'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"job.result()"
]
},
{
"cell_type": "markdown",
"id": "f0e57aa6-5573-4f07-9ac8-753cb7998091",
"metadata": {},
"source": [
"Now we can look at files available using `files` method"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "08205fd4-b3d6-44d1-a33c-fb3918c26b12",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['my_file.tar']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"available_files = serverless.files(my_function)\n",
"available_files"
]
},
{
"cell_type": "markdown",
"id": "16c3d7a6-4cce-4ef0-a1f2-e8a6d7f2c531",
"metadata": {},
"source": [
"And download them if needed using `download` method"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "39ca652d-77d7-49d2-97e9-42b60963a671",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 200/200 [00:00<00:00, 309kiB/s]\n"
]
},
{
"data": {
"text/plain": [
"'downloaded_8d3f92ba_my_file.tar'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"serverless.file_download(available_files[0], my_function)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}