Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 90 additions & 17 deletions Qiskit_qubit_measure.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8de00f2d-cbaa-4bc1-ec63-c234e49e5877"
"outputId": "250ad14d-e5f4-47a6-8756-d02f4990f0a6"
},
"execution_count": 2,
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
Expand All @@ -48,13 +48,13 @@
"Requirement already satisfied: python-dateutil>=2.8.0 in /usr/local/lib/python3.12/dist-packages (from qiskit-aer) (2.9.0.post0)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.0->qiskit-aer) (1.17.0)\n",
"Downloading qiskit-2.2.1-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (8.0 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m8.0/8.0 MB\u001b[0m \u001b[31m60.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m8.0/8.0 MB\u001b[0m \u001b[31m53.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading qiskit_aer-0.17.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.4 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.4/12.4 MB\u001b[0m \u001b[31m117.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.4/12.4 MB\u001b[0m \u001b[31m75.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading rustworkx-0.17.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m80.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m47.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading stevedore-5.5.0-py3-none-any.whl (49 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.5/49.5 kB\u001b[0m \u001b[31m3.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.5/49.5 kB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: stevedore, rustworkx, qiskit, qiskit-aer\n",
"Successfully installed qiskit-2.2.1 qiskit-aer-0.17.2 rustworkx-0.17.1 stevedore-5.5.0\n"
]
Expand All @@ -70,48 +70,121 @@
"metadata": {
"id": "gI17i0yhrQgz"
},
"execution_count": 3,
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"id": "VPOwLacGrCdU",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "619b13d9-5efc-41b6-d910-a2f58df1d7fc"
"outputId": "9ceca515-7c4c-4b36-ccbf-61475a6beb69"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"|0> counts: {'0 0': 512}\n",
"|1> counts: {'1 0': 512}\n"
"----- Task 1: Verify |0⟩ and |1⟩ -----\n",
"|0⟩ counts (512 shots): {'0 0': 512}\n",
"|1⟩ counts (512 shots): {'1 0': 512}\n",
"→ Expected: Deterministic results because |0⟩ and |1⟩ are basis states.\n",
"\n",
"----- Task 2: Change number of shots -----\n",
"|0⟩ counts (10 shots): {'0 0': 10}\n",
"|0⟩ counts (1000 shots): {'0 0': 1000}\n",
"→ Expected: No major change, deterministic measurement of |0⟩ always gives 0.\n",
"\n",
"----- Task 3: Superposition with Hadamard -----\n",
"Hadamard counts (512 shots): {'0 0': 251, '1 0': 261}\n",
"→ Expected: Probabilistic results (~50% 0 and ~50% 1) due to superposition.\n",
"\n",
"----- Task 4: Combine X and H gates -----\n",
"X + H counts (512 shots): {'0 0': 240, '1 0': 272}\n",
"→ Prediction: Applying H after X puts |1⟩ into (|0⟩ - |1⟩)/√2, so ~50% 0 and ~50% 1 but with a phase difference (not visible in measurement).\n",
"\n",
"----- Task 5 (Challenge): 2-qubit circuit -----\n",
"2-qubit counts (512 shots): {'10 00': 512}\n",
"→ Expected: Always '10' (Q1=1, Q0=0) — deterministic since both qubits are in basis states.\n",
"\n"
]
}
],
"source": [
"# Initialize |0> and |1>, measure\n",
"# Import Qiskit modules\n",
"from qiskit import QuantumCircuit, transpile\n",
"from qiskit_aer import AerSimulator\n",
"\n",
"# Initialize simulator\n",
"sim = AerSimulator()\n",
"\n",
"# |0> state\n",
"print(\"----- Task 1: Verify |0⟩ and |1⟩ -----\")\n",
"\n",
"# |0⟩ state\n",
"qc0 = QuantumCircuit(1, 1)\n",
"qc0.measure_all()\n",
"job0 = sim.run(transpile(qc0, sim), shots=512).result()\n",
"print(\"|0> counts:\", job0.get_counts())\n",
"print(\"|0 counts (512 shots):\", job0.get_counts())\n",
"\n",
"# |1> state (apply X)\n",
"# |1 state (apply X)\n",
"qc1 = QuantumCircuit(1, 1)\n",
"qc1.x(0)\n",
"qc1.measure_all()\n",
"job1 = sim.run(transpile(qc1, sim), shots=512).result()\n",
"print(\"|1> counts:\", job1.get_counts())"
"print(\"|1⟩ counts (512 shots):\", job1.get_counts())\n",
"\n",
"print(\"→ Expected: Deterministic results because |0⟩ and |1⟩ are basis states.\\n\")\n",
"\n",
"print(\"----- Task 2: Change number of shots -----\")\n",
"\n",
"# Run |0⟩ state with 10 shots\n",
"job0_10 = sim.run(transpile(qc0, sim), shots=10).result()\n",
"print(\"|0⟩ counts (10 shots):\", job0_10.get_counts())\n",
"\n",
"# Run |0⟩ state with 1000 shots\n",
"job0_1000 = sim.run(transpile(qc0, sim), shots=1000).result()\n",
"print(\"|0⟩ counts (1000 shots):\", job0_1000.get_counts())\n",
"\n",
"print(\"→ Expected: No major change, deterministic measurement of |0⟩ always gives 0.\\n\")\n",
"\n",
"\n",
"print(\"----- Task 3: Superposition with Hadamard -----\")\n",
"\n",
"qc_h = QuantumCircuit(1, 1)\n",
"qc_h.h(0)\n",
"qc_h.measure_all()\n",
"job_h = sim.run(transpile(qc_h, sim), shots=512).result()\n",
"print(\"Hadamard counts (512 shots):\", job_h.get_counts())\n",
"\n",
"print(\"→ Expected: Probabilistic results (~50% 0 and ~50% 1) due to superposition.\\n\")\n",
"\n",
"\n",
"print(\"----- Task 4: Combine X and H gates -----\")\n",
"\n",
"qc_xh = QuantumCircuit(1, 1)\n",
"qc_xh.x(0)\n",
"qc_xh.h(0)\n",
"qc_xh.measure_all()\n",
"job_xh = sim.run(transpile(qc_xh, sim), shots=512).result()\n",
"print(\"X + H counts (512 shots):\", job_xh.get_counts())\n",
"\n",
"print(\"→ Prediction: Applying H after X puts |1⟩ into (|0⟩ - |1⟩)/√2, \"\n",
" \"so ~50% 0 and ~50% 1 but with a phase difference (not visible in measurement).\\n\")\n",
"\n",
"print(\"----- Task 5 (Challenge): 2-qubit circuit -----\")\n",
"\n",
"qc2 = QuantumCircuit(2, 2)\n",
"# Qubit 0 = |0⟩ (default)\n",
"qc2.x(1) # Qubit 1 = |1⟩\n",
"qc2.measure_all()\n",
"\n",
"job2 = sim.run(transpile(qc2, sim), shots=512).result()\n",
"print(\"2-qubit counts (512 shots):\", job2.get_counts())\n",
"\n",
"print(\"→ Expected: Always '10' (Q1=1, Q0=0) — deterministic since both qubits are in basis states.\\n\")"
]
},
{
Expand All @@ -124,4 +197,4 @@
"outputs": []
}
]
}
}