{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "a694c513", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from qiskit import QuantumCircuit" ] }, { "cell_type": "code", "execution_count": null, "id": "f4f2f294", "metadata": {}, "outputs": [], "source": [ "# 1. A quantum circuit for preparing the quantum state |000> + i |111> / √2\n", "qc = QuantumCircuit(3)\n", "qc.h(0) # generate superposition\n", "qc.p(np.pi / 2, 0) # add quantum phase\n", "qc.cx(0, 1) # 0th-qubit-Controlled-NOT gate on 1st qubit\n", "qc.cx(0, 2) # 0th-qubit-Controlled-NOT gate on 2nd qubit" ] }, { "cell_type": "code", "execution_count": null, "id": "1ccd51de-36af-4a76-bf9a-2799f8f963bc", "metadata": {}, "outputs": [], "source": [ "# 2. Add the classical output in the form of measurement of all qubits\n", "qc_measured = qc.measure_all(inplace=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "d083ce1e-2d27-44ef-b503-1816ea48d5dd", "metadata": {}, "outputs": [], "source": [ "# 3. Execute using the Sampler primitive\n", "from qiskit.primitives import StatevectorSampler\n", "sampler = StatevectorSampler()\n", "job = sampler.run([qc_measured], shots=1000)\n", "result = job.result()\n", "print(f\" > Counts: {result[0].data['meas'].get_counts()}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "3fa4227a-152a-4c79-b2e3-b87239bfbb96", "metadata": {}, "outputs": [], "source": [ "# 4. Define the observable to be measured \n", "from qiskit.quantum_info import SparsePauliOp\n", "operator = SparsePauliOp.from_list([(\"XXY\", 1), (\"XYX\", 1), (\"YXX\", 1), (\"YYY\", -1)])" ] }, { "cell_type": "code", "execution_count": null, "id": "b64350b2-4dfa-4be3-96fa-48f21fdbd8fd", "metadata": {}, "outputs": [], "source": [ "# 5. Execute using the Estimator primitive\n", "from qiskit.primitives import StatevectorEstimator\n", "estimator = StatevectorEstimator()\n", "job = estimator.run([(qc, operator)], precision=1e-3)\n", "result = job.result()\n", "print(f\" > Expectation values: {result[0].data.evs}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "c01f685b-e010-4964-95e2-0eab528735be", "metadata": {}, "outputs": [], "source": [] } ], "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.12.10" } }, "nbformat": 4, "nbformat_minor": 5 }