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
36 changes: 19 additions & 17 deletions quantum-applications-to-finance/solutions/02_solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,30 @@
"# maps |x> to |x+1> mod 2**n for arbitrary n\n",
"\n",
"@cudaq.kernel\n",
"def inc_with_n_qubits(qubits : cudaq.qview, num_qubits : int):\n",
" # EDIT THE CODE BELOW\n",
" for n in range(num_qubits):\n",
" x.ctrl(qubits[n+1:num_qubits], qubits[n])\n",
" # EDIT THE CODE ABOVE\n",
" \n",
"def inc_with_n_qubits(qubits: cudaq.qview):\n",
" n = len(qubits)\n",
" for i in range(n):\n",
" if i + 1 < n:\n",
" x.ctrl(qubits[i+1:n], qubits[i])\n",
" else:\n",
" x(qubits[i])\n",
"\n",
"# Define a kernel on n qubits for the dec operation that\n",
"# maps |x> to |x-1> mod 2**n\n",
"@cudaq.kernel\n",
"def dec(qubits : cudaq.qview):\n",
" cudaq.adjoint(inc_with_n_qubits, qubits, num_qubits) \n",
" \n",
" cudaq.adjoint(inc_with_n_qubits, qubits) \n",
"\n",
"# A kernel to test your solution\n",
"@cudaq.kernel()\n",
"def test(binary_list : list[int], num_qubits : int):\n",
" qubits = cudaq.qvector(num_qubits)\n",
"def test(binary_list : list[int]):\n",
" qubits = cudaq.qvector(len(binary_list))\n",
" # Initialize the qubits to the initial position\n",
" for n in range(len(binary_list)):\n",
" if binary_list[n] == 1:\n",
" x(qubits[n])\n",
" # Move from position |x> to |x+1>\n",
" inc_with_n_qubits(qubits, num_qubits)\n",
" inc_with_n_qubits(qubits)\n",
"\n",
"# Code to verify that the kernel acts as required\n",
"def int_to_binary_list(n, num_bits):\n",
Expand Down Expand Up @@ -103,9 +105,9 @@
"\n",
"num_qubits = 5\n",
"for i in range(2**num_qubits):\n",
" initial_position = i\n",
" result = cudaq.sample(test, int_to_binary_list(i, num_qubits), num_qubits).most_probable()\n",
" print(f\"Initial position: {initial_position}, Result: {result}\")\n",
" binary_input = int_to_binary_list(i, num_qubits)\n",
" result = cudaq.sample(test, binary_input).most_probable()\n",
" print(f\"Initial position: {i:02d}, Result: {result}\")\n",
" "
]
},
Expand Down Expand Up @@ -154,15 +156,15 @@
" no_DEC_at_left_endpoint(walker_qubits, coin_qubit, endpoint_qubit)\n",
" \n",
" # Apply the dec if the the coin is in |0>\n",
" x(coin_qubit) \n",
" x(coin_qubit)\n",
" cudaq.control(dec, coin_qubit, walker_qubits)\n",
" x(coin_qubit) \n",
" x(coin_qubit)\n",
" \n",
" # Reset the coin and endpoints in case they were changed to avoid moving from |0000> to |1111>\n",
" reset_coin_and_endpoint(coin_qubit, endpoint_qubit)\n",
" \n",
"@cudaq.kernel()\n",
"def SSQW_with_measurement(num_qubits : int, num_time_steps : int): \n",
"def SSQW_with_measurement(num_qubits : int, num_time_steps : int):\n",
" walker_qubits = cudaq.qvector(num_qubits)\n",
" coin_qubit = cudaq.qubit()\n",
" endpoint_qubit = cudaq.qubit()\n",
Expand Down