{ "cells": [ { "cell_type": "markdown", "id": "f9e40036", "metadata": {}, "source": [ "## How to generate exact quasiprobability distributions from Sampler\n", "\n", "This how-to guide is intended to show users how they can generate statevector-based quasiprobability distributions through the `qiskit.primitives.BaseSampler` interface." ] }, { "cell_type": "code", "execution_count": 1, "id": "072055cb", "metadata": {}, "outputs": [], "source": [ "from qiskit import QuantumCircuit\n", "from qiskit.quantum_info import SparsePauliOp\n", "\n", "from qiskit_addon_cutting import (\n", " partition_problem,\n", " generate_cutting_experiments,\n", ")" ] }, { "cell_type": "markdown", "id": "940334fd", "metadata": {}, "source": [ "Prepare inputs to `generate_cutting_experiments`" ] }, { "cell_type": "code", "execution_count": 2, "id": "dc4af922", "metadata": {}, "outputs": [], "source": [ "circuit = QuantumCircuit(2)\n", "circuit.h(0)\n", "circuit.cx(0, 1)\n", "observable = SparsePauliOp([\"ZZ\"])\n", "partitioned_problem = partition_problem(\n", " circuit=circuit, partition_labels=\"AB\", observables=observable.paulis\n", ")\n", "subcircuits = partitioned_problem.subcircuits\n", "subobservables = partitioned_problem.subobservables" ] }, { "cell_type": "markdown", "id": "d6361a9d", "metadata": {}, "source": [ "Call `generate_cutting_experiments`" ] }, { "cell_type": "code", "execution_count": 3, "id": "d095701f", "metadata": {}, "outputs": [], "source": [ "subexperiments, coefficients = generate_cutting_experiments(\n", " circuits=subcircuits,\n", " observables=subobservables,\n", " num_samples=1000,\n", ")" ] }, { "cell_type": "markdown", "id": "bc59b1be", "metadata": {}, "source": [ "In order to calculate exact quasiprobability distributions for circuits with mid-circuit measurements, users will need to use the `ExactSampler` class from `qiskit_addon_cutting.utils.simulation`. The Qiskit Samplers do not support mid-circuit measurements in statevector mode." ] }, { "cell_type": "code", "execution_count": 4, "id": "7a74f709", "metadata": {}, "outputs": [], "source": [ "from qiskit_addon_cutting.utils.simulation import ExactSampler\n", "\n", "exact_sampler = ExactSampler()" ] }, { "cell_type": "markdown", "id": "8d050fbe", "metadata": {}, "source": [ "If `ExactSampler` is used, the quasiprobability distributions returned from `generate_cutting_experiments` will be exact and generated from the statevectors of the subexperiments." ] }, { "cell_type": "code", "execution_count": 5, "id": "7019d781", "metadata": {}, "outputs": [], "source": [ "results = {\n", " label: exact_sampler.run(subexperiment).result()\n", " for label, subexperiment in subexperiments.items()\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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": 5 }