{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Projects & Properties & Context Management\n", "\n", "Nexus provides several ways to organize your data, store custom parameters and integrate these easily into your workflows.\n", "\n", "- Projects are a way of categorizing and containing your workflows and data, a bit like a folder in a filesystem.\n", "\n", "- Properties are a place to store additional custom metadata or scientific parameters. Properties are defined on a Project, and given values for data within that project.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "import qnexus as qnx\n", "\n", "from pytket import Circuit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get a ProjectRef \n", "my_project_ref = qnx.projects.create(name=f\"My Project from {datetime.now()}\")\n", "\n", "# Show basic information about the project\n", "my_project_ref.df()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get a summary of the project as a DataFrame (shows Job statuses)\n", "qnx.projects.summarize(my_project_ref)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Lets add a custom property \n", "\n", "qnx.projects.add_property(\n", " name=\"molecule_type\",\n", " property_type=\"string\",\n", " project=my_project_ref,\n", " description=\"The type of molecule being studied\",\n", ")\n", "\n", "qnx.projects.add_property(\n", " name=\"iteration\",\n", " property_type=\"int\",\n", " project=my_project_ref,\n", " description=\"The iteration number of the experiment\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# View the properties defined on the project as a DataFrame\n", "\n", "qnx.projects.get_properties(project=my_project_ref).df()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# We can then define property values on data we create within the project\n", "\n", "my_circuit_ref = qnx.circuits.upload(\n", " name=f\"My Circuit from {datetime.now()}\",\n", " circuit = Circuit(2).ZZPhase(0.5, 0, 1).measure_all(),\n", " project = my_project_ref,\n", " properties={\"molecule_type\": \"H2\"},\n", ")\n", "\n", "my_circuit_ref.df()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you submit a Job to Nexus with defined property values, these values will be propogated to the outputs of the Job." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Context Management\n", "\n", "If we know we'll be working within a project or with a set of defined property values we can provide these in a system of context management." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Via a typical context manager\n", "with qnx.context.using_project(my_project_ref):\n", " # Show all circuits in the project\n", " qnx.circuits.get_all().df()\n", "\n", " # These values can be overridden, with parameters taking precedence over context\n", " my_circuit_ref = qnx.circuits.upload(\n", " name=f\"My Circuit from {datetime.now()}\",\n", " circuit = Circuit(2).ZZPhase(0.5, 0, 1).measure_all(),\n", " project=qnx.projects.create(name=f\"My other Nexus Project from {datetime.now()}\")\n", " )\n", "\n", " with qnx.context.using_properties(molecule_type = \"H2\", iteration = 0):\n", "\n", " # Create a circuit within the project and with defined property values\n", " my_circuit_ref = qnx.circuits.upload(\n", " name=f\"My Circuit from {datetime.now()}\",\n", " circuit = Circuit(2).ZZPhase(0.5, 0, 1).measure_all(),\n", " )\n", "\n", " # Property parameters will be the union of the parameters and context (parameters take precedence)\n", " my_circuit_ref = qnx.circuits.upload(\n", " name=f\"My Circuit from {datetime.now()}\",\n", " circuit = Circuit(2).ZZPhase(0.5, 0, 1).measure_all(),\n", " properties={\"iteration\": 1},\n", " )\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Values can also be set in the global context\n", "\n", "current_project_context = qnx.context.set_active_project_token(my_project_ref)\n", "\n", "# Deactivate the project context\n", "\n", "qnx.context.deactivate_project(current_project_context)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Project Updating & Deletion\n", "\n", "`qnexus` can be used to update or delete a project. Updating will allow you to change details such as the project's name, or set it as 'archived' (meaning it will no longer show up by default in the UI or when retrieving projects via the API).\n", "\n", "Projects can also be deleted, which will permanently and irreversibly purge any project data stored by Nexus (including circuits and results). This will free up Storage quota for all the resources within the project.\n", "\n", "**NB**:\n", "- projects must be archived before they can be deleted.\n", "- project deletion won't cancel any jobs submitted to either H-series or third parties - so make sure that any running jobs are cancelled to be safe.\n", "- project deletion will delete the data for all contributors to the project, so double check with your team members before deleting any data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# update the project\n", "updated_project_ref = qnx.projects.update(\n", " project=my_project_ref,\n", " archive=True,\n", ")\n", "\n", "# Permanently delete the project and all underlying data for everyone\n", "qnx.projects.delete(project=updated_project_ref)" ] } ], "metadata": { "kernelspec": { "display_name": "qnexus-Rou6F43i-py3.11", "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.8" } }, "nbformat": 4, "nbformat_minor": 2 }