diff --git a/.gitignore b/.gitignore index 55759ac..8b36043 100644 --- a/.gitignore +++ b/.gitignore @@ -215,4 +215,5 @@ __marimo__/ # Streamlit .streamlit/secrets.toml -/solution.py \ No newline at end of file +/solution.py +.idea/ \ No newline at end of file diff --git a/task.ipynb b/task.ipynb index 81e5b05..fef7cb1 100644 --- a/task.ipynb +++ b/task.ipynb @@ -38,14 +38,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:43.117206Z", + "start_time": "2025-09-25T16:34:41.606113Z" + } + }, "source": [ "from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile\n", "from qiskit_aer import AerSimulator\n", "import numpy as np" - ] + ], + "outputs": [], + "execution_count": 1 }, { "cell_type": "markdown", @@ -67,9 +72,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:43.268701Z", + "start_time": "2025-09-25T16:34:43.155497Z" + } + }, "source": [ "def create_basic_circuit():\n", " \"\"\"\n", @@ -83,7 +91,10 @@ " QuantumCircuit: A 2-qubit quantum circuit.\n", " \"\"\"\n", " qc = QuantumCircuit(2, 2)\n", - " \n", + " qc.x(0)\n", + " qc.h(1)\n", + " qc.barrier()\n", + " qc.measure(range(2),range(2))\n", " # Put qubit 0 in the |1⟩ state.\n", " \n", " # Put qubit 1 in a superposition state.\n", @@ -105,7 +116,25 @@ "result = job.result()\n", "counts = result.get_counts()\n", "print(\"\\nMeasurement results:\", counts)\n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " ┌───┐ ░ ┌─┐ \n", + "q_0: ┤ X ├─░─┤M├───\n", + " ├───┤ ░ └╥┘┌─┐\n", + "q_1: ┤ H ├─░──╫─┤M├\n", + " └───┘ ░ ║ └╥┘\n", + "c: 2/═════════╩══╩═\n", + " 0 1 \n", + "\n", + "Measurement results: {'01': 525, '11': 499}\n" + ] + } + ], + "execution_count": 2 }, { "cell_type": "markdown", @@ -120,9 +149,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:44.657756Z", + "start_time": "2025-09-25T16:34:44.644398Z" + } + }, "source": [ "def create_plus_state():\n", " \"\"\"\n", @@ -136,7 +168,8 @@ " qc = QuantumCircuit(1, 1)\n", " # Apply the appropriate operation to prepare the |+⟩ state.\n", " # Measure the qubit.\n", - " \n", + " qc.h(0)\n", + " qc.measure(0,0)\n", " return qc\n", "\n", "def create_minus_state():\n", @@ -151,7 +184,10 @@ " qc = QuantumCircuit(1, 1)\n", " # Apply the required operations to prepare the |−⟩ state.\n", " # Measure the qubit.\n", - " \n", + " qc.h(0)\n", + " qc.s(0)\n", + " qc.s(0)\n", + " qc.measure(0,0)\n", " return qc\n", "\n", "def create_bell_state():\n", @@ -166,15 +202,22 @@ " qc = QuantumCircuit(2, 2)\n", " # Apply the operations to create the entangled state.\n", " # Measure both qubits.\n", - " \n", + " qc.h(0)\n", + " qc.cx(0,1)\n", + " qc.measure(range(2),range(2))\n", " return qc\n" - ] + ], + "outputs": [], + "execution_count": 3 }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:44.728527Z", + "start_time": "2025-09-25T16:34:44.689342Z" + } + }, "source": [ "# Test each state\n", "states = {\n", @@ -186,13 +229,47 @@ "for name, circuit in states.items():\n", " print(f\"\\n{name}:\")\n", " print(circuit.draw())\n", - " \n", " job = simulator.run(circuit, shots=1024)\n", " result = job.result()\n", " counts = result.get_counts()\n", " print(\"Results:\", counts)\n", " \n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Plus State:\n", + " ┌───┐┌─┐\n", + " q: ┤ H ├┤M├\n", + " └───┘└╥┘\n", + "c: 1/══════╩═\n", + " 0 \n", + "Results: {'0': 506, '1': 518}\n", + "\n", + "Minus State:\n", + " ┌───┐┌───┐┌───┐┌─┐\n", + " q: ┤ H ├┤ S ├┤ S ├┤M├\n", + " └───┘└───┘└───┘└╥┘\n", + "c: 1/════════════════╩═\n", + " 0 \n", + "Results: {'0': 475, '1': 549}\n", + "\n", + "Bell State:\n", + " ┌───┐ ┌─┐ \n", + "q_0: ┤ H ├──■──┤M├───\n", + " └───┘┌─┴─┐└╥┘┌─┐\n", + "q_1: ─────┤ X ├─╫─┤M├\n", + " └───┘ ║ └╥┘\n", + "c: 2/═══════════╩══╩═\n", + " 0 1 \n", + "Results: {'00': 500, '11': 524}\n" + ] + } + ], + "execution_count": 4 }, { "cell_type": "markdown", @@ -207,9 +284,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:44.779585Z", + "start_time": "2025-09-25T16:34:44.767399Z" + } + }, "source": [ "def create_constant_oracle(output):\n", " \"\"\"\n", @@ -225,10 +305,9 @@ " QuantumCircuit: A 2-qubit circuit representing the oracle.\n", " \"\"\"\n", " oracle = QuantumCircuit(2, name=f'f(x)={output}')\n", - " \n", " if output == 1:\n", " # Apply the required operation to the ancilla (qubit 1).\n", - " pass\n", + " oracle.x(1)\n", " \n", " return oracle\n", "\n", @@ -240,7 +319,39 @@ "print(oracle_0.draw())\n", "print(\"\\nConstant-1 Oracle:\") \n", "print(oracle_1.draw())\n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Constant-0 Oracle:\n", + " \n", + "q_0: \n", + " \n", + "q_1: \n", + " \n", + "\n", + "Constant-1 Oracle:\n", + " \n", + "q_0: ─────\n", + " ┌───┐\n", + "q_1: ┤ X ├\n", + " └───┘\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "<>:5: SyntaxWarning: invalid escape sequence '\\o'\n", + "<>:5: SyntaxWarning: invalid escape sequence '\\o'\n", + "C:\\Users\\PaccoTan\\AppData\\Local\\Temp\\ipykernel_26040\\895320350.py:5: SyntaxWarning: invalid escape sequence '\\o'\n", + " The oracle is a unitary operation that maps $|x⟩|y⟩$ to $|x⟩|y \\oplus f(x)⟩$.\n" + ] + } + ], + "execution_count": 5 }, { "cell_type": "markdown", @@ -258,9 +369,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:44.849673Z", + "start_time": "2025-09-25T16:34:44.842506Z" + } + }, "source": [ "def create_constant_0_oracle():\n", " \"\"\"\n", @@ -280,6 +394,7 @@ " QuantumCircuit: A 2-qubit circuit with a gate on the ancilla.\n", " \"\"\" \n", " oracle = QuantumCircuit(2, name='Constant-1')\n", + " oracle.x(1)\n", " # Apply the operation to the ancilla qubit.\n", " return oracle\n", "\n", @@ -291,7 +406,9 @@ " QuantumCircuit: A 2-qubit circuit with a controlled gate.\n", " \"\"\"\n", " oracle = QuantumCircuit(2, name='Balanced-Identity')\n", + " \n", " # Apply the appropriate controlled operation.\n", + " oracle.cx(0,1)\n", " return oracle\n", "\n", "def create_balanced_not_oracle():\n", @@ -303,14 +420,21 @@ " \"\"\"\n", " oracle = QuantumCircuit(2, name='Balanced-NOT')\n", " # Apply the required operations to the qubits.\n", + " oracle.cx(0,1)\n", + " oracle.x(1)\n", " return oracle\n" - ] + ], + "outputs": [], + "execution_count": 6 }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:44.883013Z", + "start_time": "2025-09-25T16:34:44.859903Z" + } + }, "source": [ "# Test your oracles by visualizing them\n", "oracles = {\n", @@ -324,7 +448,44 @@ " print(f\"{name}:\")\n", " print(oracle.draw())\n", " print()\n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Constant 0:\n", + " \n", + "q_0: \n", + " \n", + "q_1: \n", + " \n", + "\n", + "Constant 1:\n", + " \n", + "q_0: ─────\n", + " ┌───┐\n", + "q_1: ┤ X ├\n", + " └───┘\n", + "\n", + "Balanced Identity:\n", + " \n", + "q_0: ──■──\n", + " ┌─┴─┐\n", + "q_1: ┤ X ├\n", + " └───┘\n", + "\n", + "Balanced NOT:\n", + " \n", + "q_0: ──■───────\n", + " ┌─┴─┐┌───┐\n", + "q_1: ┤ X ├┤ X ├\n", + " └───┘└───┘\n", + "\n" + ] + } + ], + "execution_count": 7 }, { "cell_type": "markdown", @@ -335,9 +496,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:44.964988Z", + "start_time": "2025-09-25T16:34:44.954297Z" + } + }, "source": [ "def deutsch_algorithm(oracle):\n", " \"\"\"\n", @@ -359,15 +523,19 @@ " qc = QuantumCircuit(2, 1)\n", " \n", " # Step 1: Initialize the ancilla.\n", - " \n", + " qc.x(1)\n", + " qc.barrier()\n", " # Step 2: Apply the superposition-creating gate to both qubits.\n", - " \n", + " qc.h(range(2))\n", + " qc.barrier()\n", " # Step 3: Apply the oracle.\n", - " \n", + " qc = qc.compose(oracle)\n", + " qc.barrier()\n", " # Step 4: Apply a final superposition-creating gate to the input qubit only.\n", - " \n", + " qc.h(0)\n", + " qc.barrier()\n", " # Step 5: Measure the input qubit.\n", - " \n", + " qc.measure(0,0)\n", " return qc\n", "\n", "# Visualize the complete algorithm with one oracle\n", @@ -375,13 +543,33 @@ "complete_circuit = deutsch_algorithm(sample_oracle)\n", "print(\"Complete Deutsch Algorithm:\")\n", "print(complete_circuit.draw())\n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Complete Deutsch Algorithm:\n", + " ░ ┌───┐ ░ ░ ┌───┐ ░ ┌─┐\n", + "q_0: ──────░─┤ H ├─░───■───░─┤ H ├─░─┤M├\n", + " ┌───┐ ░ ├───┤ ░ ┌─┴─┐ ░ └───┘ ░ └╥┘\n", + "q_1: ┤ X ├─░─┤ H ├─░─┤ X ├─░───────░──╫─\n", + " └───┘ ░ └───┘ ░ └───┘ ░ ░ ║ \n", + "c: 1/═════════════════════════════════╩═\n", + " 0 \n" + ] + } + ], + "execution_count": 8 }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:45.120461Z", + "start_time": "2025-09-25T16:34:45.081105Z" + } + }, "source": [ "def test_deutsch_algorithm():\n", " \"\"\"Test Deutsch algorithm with all four oracles\"\"\"\n", @@ -415,7 +603,34 @@ "\n", "\n", "test_deutsch_algorithm()\n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deutsch Algorithm Test Results:\n", + "==================================================\n", + "Constant 0:\n", + " Results: {'0': 1024}\n", + " Type: CONSTANT\n", + "\n", + "Constant 1:\n", + " Results: {'0': 1024}\n", + " Type: CONSTANT\n", + "\n", + "Balanced (x):\n", + " Results: {'1': 1024}\n", + " Type: BALANCED\n", + "\n", + "Balanced (1⊕x):\n", + " Results: {'1': 1024}\n", + " Type: BALANCED\n", + "\n" + ] + } + ], + "execution_count": 9 }, { "cell_type": "markdown", @@ -446,9 +661,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:45.238770Z", + "start_time": "2025-09-25T16:34:45.222444Z" + } + }, "source": [ "def create_dj_oracle(n, function_type, mask=None):\n", " \"\"\"\n", @@ -476,6 +694,7 @@ " if function_type == 'constant':\n", " if mask == '1': \n", " # Apply the appropriate operation to the ancilla (qubit n).\n", + " oracle.x(n)\n", " pass\n", " \n", " elif function_type == 'balanced':\n", @@ -483,7 +702,7 @@ " # Iterate through the mask and apply a controlled operation for each '1' bit.\n", " for i, bit in enumerate(mask):\n", " if bit == '1':\n", - " pass\n", + " oracle.cx(i,n)\n", " \n", " return oracle\n", "\n", @@ -491,7 +710,36 @@ "test_oracle = create_dj_oracle(3, 'balanced', '101')\n", "print(\"3-qubit balanced oracle (mask='101'):\")\n", "print(test_oracle.draw())\n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3-qubit balanced oracle (mask='101'):\n", + " \n", + "q_0: ──■───────\n", + " │ \n", + "q_1: ──┼───────\n", + " │ \n", + "q_2: ──┼────■──\n", + " ┌─┴─┐┌─┴─┐\n", + "q_3: ┤ X ├┤ X ├\n", + " └───┘└───┘\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "<>:11: SyntaxWarning: invalid escape sequence '\\s'\n", + "<>:11: SyntaxWarning: invalid escape sequence '\\s'\n", + "C:\\Users\\PaccoTan\\AppData\\Local\\Temp\\ipykernel_26040\\149676844.py:11: SyntaxWarning: invalid escape sequence '\\s'\n", + " This implements the inner product $f(x) = \\sum_{i} x_i \\cdot mask_i \\pmod{2}$.\n" + ] + } + ], + "execution_count": 10 }, { "cell_type": "markdown", @@ -502,9 +750,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:45.314155Z", + "start_time": "2025-09-25T16:34:45.302023Z" + } + }, "source": [ "def deutsch_jozsa_algorithm(n, oracle):\n", " \"\"\"\n", @@ -527,22 +778,47 @@ " qc = QuantumCircuit(n + 1, n)\n", " \n", " # Step 1: Initialize the ancilla.\n", - " \n", + " qc.x(n)\n", + " qc.barrier()\n", " # Step 2: Apply the superposition-creating gate to all qubits.\n", - " \n", + " qc.h(range(n+1))\n", + " qc.barrier()\n", " # Step 3: Apply the oracle.\n", - " \n", + " qc = qc.compose(oracle)\n", + " qc.barrier()\n", " # Step 4: Apply the final superposition-creating gate to the input qubits.\n", - " \n", + " qc.h(range(n))\n", + " qc.barrier()\n", " # Step 5: Measure the input qubits.\n", - " \n", + " qc.measure(range(n),range(n))\n", " return qc\n", "\n", "# Test with the oracle from above\n", "test_circuit = deutsch_jozsa_algorithm(3, test_oracle)\n", "print(\"Complete 3-qubit Deutsch-Jozsa circuit:\")\n", "print(test_circuit.draw())\n" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Complete 3-qubit Deutsch-Jozsa circuit:\n", + " ░ ┌───┐ ░ ░ ┌───┐ ░ ┌─┐ \n", + "q_0: ──────░─┤ H ├─░───■────────░─┤ H ├─░─┤M├──────\n", + " ░ ├───┤ ░ │ ░ ├───┤ ░ └╥┘┌─┐ \n", + "q_1: ──────░─┤ H ├─░───┼────────░─┤ H ├─░──╫─┤M├───\n", + " ░ ├───┤ ░ │ ░ ├───┤ ░ ║ └╥┘┌─┐\n", + "q_2: ──────░─┤ H ├─░───┼────■───░─┤ H ├─░──╫──╫─┤M├\n", + " ┌───┐ ░ ├───┤ ░ ┌─┴─┐┌─┴─┐ ░ └───┘ ░ ║ ║ └╥┘\n", + "q_3: ┤ X ├─░─┤ H ├─░─┤ X ├┤ X ├─░───────░──╫──╫──╫─\n", + " └───┘ ░ └───┘ ░ └───┘└───┘ ░ ░ ║ ║ ║ \n", + "c: 3/══════════════════════════════════════╩══╩══╩═\n", + " 0 1 2 \n" + ] + } + ], + "execution_count": 11 }, { "cell_type": "markdown", @@ -553,9 +829,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:45.465352Z", + "start_time": "2025-09-25T16:34:45.351975Z" + } + }, "source": [ "def test_deutsch_jozsa():\n", " \"\"\"Test with multiple cases\"\"\"\n", @@ -575,6 +854,9 @@ " print(f\"\\nTest: n={n}, {func_type}, mask={mask}\")\n", " print(f\"Expected: {expected}\")\n", " \n", + " if mask is not None:\n", + " mask = mask[::-1]\n", + " \n", " oracle = create_dj_oracle(n, func_type, mask)\n", " circuit = deutsch_jozsa_algorithm(n, oracle)\n", " \n", @@ -596,7 +878,60 @@ "\n", "# Run comprehensive test\n", "test_deutsch_jozsa()" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deutsch-Jozsa Test Results:\n", + "============================================================\n", + "\n", + "Test: n=2, constant, mask=None\n", + "Expected: Should measure 00\n", + "Results: {'00': 1024}\n", + "Most frequent: 00\n", + "Correct: True\n", + "\n", + "Test: n=2, constant, mask=1\n", + "Expected: Should measure 00\n", + "Results: {'00': 1024}\n", + "Most frequent: 00\n", + "Correct: True\n", + "\n", + "Test: n=3, balanced, mask=101\n", + "Expected: Should measure 101\n", + "Results: {'101': 1024}\n", + "Most frequent: 101\n", + "Correct: True\n", + "\n", + "Test: n=3, balanced, mask=111\n", + "Expected: Should measure 111\n", + "Results: {'111': 1024}\n", + "Most frequent: 111\n", + "Correct: True\n", + "\n", + "Test: n=2, balanced, mask=01\n", + "Expected: Should measure 01\n", + "Results: {'01': 1024}\n", + "Most frequent: 01\n", + "Correct: True\n" + ] + } + ], + "execution_count": 12 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2025-09-25T16:34:45.555344Z", + "start_time": "2025-09-25T16:34:45.550486Z" + } + }, + "cell_type": "code", + "source": "", + "outputs": [], + "execution_count": null } ], "metadata": { @@ -611,6 +946,11 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3 + }, + "kernelspec": { + "name": "python3", + "language": "python", + "display_name": "Python 3 (ipykernel)" } }, "nbformat": 4,