Skip to content

Commit

Permalink
Add solutions of coding assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
artix41 committed Jan 11, 2020
1 parent 8d4bc71 commit 31e137f
Show file tree
Hide file tree
Showing 17 changed files with 5,379 additions and 0 deletions.

Large diffs are not rendered by default.

349 changes: 349 additions & 0 deletions coding_assignments/solutions/02_Measurements_and_Mixed_States.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,349 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before you begin, execute this cell to import numpy and packages from the D-Wave Ocean suite, and all necessary functions for the gate-model framework you are going to use, whether that is the Forest SDK or Qiskit. In the case of Forest SDK, it also starts the qvm and quilc servers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%run -i \"assignment_helper.py\"\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Measurements\n",
"\n",
"**Exercise 1** (1 point). Measurements in the quantum computers we can access today always measure in the computational basis. This means that either the projection $|0\\rangle\\langle 0|$ or the $|1\\rangle\\langle 1|$ is applied on the qubit we are measuring, corresponding to the outcome 0 or 1. The Born rule tells us that we get either of these with the probability of the absolute value of the probability amplitude squared. The qubit afterwards is $|0\\rangle$ or $|1\\rangle$, respectively.\n",
"\n",
"First, create a circuit in your preferred framework with two classical registers (and one quantum register if you're using Qiskit). The object should be called `circuit`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"q = QuantumRegister(1)\n",
"c = ClassicalRegister(2)\n",
"circuit = QuantumCircuit(q, c)\n",
"# Forest version\n",
"# circuit = Program()\n",
"# ro = circuit.declare('ro', 'BIT', 2)\n",
"### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"classical_bits = get_classical_bits(circuit)\n",
"assert classical_bits == 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 2** (1 point). Next, extend the circuit with a Hadamard gate and a measurement on the qubit. Write the measurement result in the first classical register. You will get a probabilistic outcome of 0 or 1."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"circuit.h(q[0])\n",
"circuit.measure(q[0], c[0])\n",
"# Forest version\n",
"# circuit += H(0)\n",
"# circuit += MEASURE(0, ro[0])\n",
"### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-11-19T19:48:31.136144Z",
"start_time": "2018-11-19T19:48:30.043322Z"
}
},
"outputs": [],
"source": [
"counts = get_single_measurement_counts(circuit)\n",
"assert abs(counts['00']/100-.5) < 0.1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 3** (1 point). To see that the quantum state collapses to the basis state indicated by the outcome, apply a second measurement on the same circuit, but write the result in the second register. Executing the program should always give the same outcome in the two subsequent measurements."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-11-19T19:48:31.147404Z",
"start_time": "2018-11-19T19:48:31.139100Z"
}
},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"circuit.measure(q[0], c[1])\n",
"# Forest version\n",
"# circuit += MEASURE(0, ro[1])\n",
"### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"counts = get_counts(circuit)\n",
"assert abs(counts['00']/100-.5) < 0.1\n",
"assert abs(counts['11']/100-.5) < 0.1\n",
"assert sum(counts.values()) == 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Measuring multiqubit systems\n",
"\n",
"**Exercise 4** (1 point). We typically work with local measurements, meaning that the qubits are measured separately. Create a two-qubit circuit with measurements included that reproduces the uniform distribution on four outcomes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-11-19T19:48:31.367295Z",
"start_time": "2018-11-19T19:48:31.193956Z"
}
},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"q = QuantumRegister(2)\n",
"c = ClassicalRegister(2)\n",
"circuit = QuantumCircuit(q, c)\n",
"circuit.h(q[0])\n",
"circuit.h(q[1])\n",
"circuit.measure(q, c)\n",
"# Forest version\n",
"# circuit = Program()\n",
"# ro = circuit.declare('ro', 'BIT', 2)\n",
"# circuit += H(0)\n",
"# circuit += H(1)\n",
"# circuit += MEASURE(0, ro[0])\n",
"# circuit += MEASURE(1, ro[1])\n",
"### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"counts = get_counts(circuit)\n",
"assert abs(counts['00']/100-.25) < 0.1\n",
"assert abs(counts['01']/100-.25) < 0.1\n",
"assert abs(counts['11']/100-.25) < 0.1\n",
"assert sum(counts.values()) == 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 5** (1 point). This is a typical product state, showing no correlation between the qubits. In contrast, if you make simultaneous measurements on an entangled state, for instance the $|\\phi^+\\rangle$ or the $|\\phi^-\\rangle$ state, the measurement outcomes will be correlated. Create either of these states and measure both qubits."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-11-19T19:48:31.507651Z",
"start_time": "2018-11-19T19:48:31.369445Z"
}
},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"q = QuantumRegister(2)\n",
"c = ClassicalRegister(2)\n",
"circuit = QuantumCircuit(q, c)\n",
"circuit.h(q[0])\n",
"circuit.cx(q[0], q[1])\n",
"circuit.measure(q, c)\n",
"# Forest\n",
"# circuit = Program()\n",
"# ro = circuit.declare('ro', 'BIT', 2)\n",
"# circuit += H(0)\n",
"# circuit += CNOT(0, 1)\n",
"# circuit += MEASURE(0, ro[0])\n",
"# circuit += MEASURE(1, ro[1])\n",
"### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"counts = get_counts(circuit)\n",
"assert abs(counts['00']/100-.5) < 0.1\n",
"assert abs(counts['11']/100-.5) < 0.1\n",
"assert sum(counts.values()) == 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that we only observe 00 and 11 as outcomes, even though we made measurements on two spatially separated qubits. If one measurement gives a value, the other one always give the same. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mixed states\n",
"\n",
"**Exercise 6** (1 point). The density matrix formalism is critical in understanding noise models and decoherence, so it is indispensible to start working with current and near-future quantum computers. Many simulator backends provide noise models, but it is out of scope for this course to develop a deep understanding of it.\n",
"\n",
"Instead, let's use the wavefunction simulator to create the (maximally) entangled state $|\\phi^+\\rangle$. The object should be called circuit and do not include measurements. Your circuit should be in an object called `circuit`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-11-19T19:48:31.519211Z",
"start_time": "2018-11-19T19:48:31.509701Z"
}
},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"q = QuantumRegister(2)\n",
"c = ClassicalRegister(2)\n",
"circuit = QuantumCircuit(q, c)\n",
"circuit.h(q[0])\n",
"circuit.cx(q[0], q[1])\n",
"# Forest version\n",
"# circuit = Program()\n",
"# ro = circuit.declare('ro', 'BIT', 2)\n",
"# circuit += H(0)\n",
"# circuit += CNOT(0, 1)\n",
"### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"amplitudes = get_amplitudes(circuit)\n",
"assert np.allclose(np.array([np.sqrt(2)/2, 0, 0, np.sqrt(2)/2]), amplitudes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 7** (1 point). Create the density matrix called `dm` of the probability amplitudes contained in the `amplitudes` array. Recall that you need a ket and a bra in this order to produce it, but the shape of the `amplitudes` array is incorrect for transposition. Reshape it first"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"amplitudes = amplitudes.reshape((4,1))\n",
"dm = amplitudes.dot(amplitudes.T.conj())\n",
"### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-11-19T19:48:31.537553Z",
"start_time": "2018-11-19T19:48:31.521246Z"
}
},
"outputs": [],
"source": [
"### BEGIN HIDDEN TESTS\n",
"target = np.array([[0.5+0.j, 0. +0.j, 0. +0.j, 0.5+0.j],\n",
" [0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j],\n",
" [0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j],\n",
" [0.5+0.j, 0. +0.j, 0. +0.j, 0.5+0.j]])\n",
"assert np.alltrue(np.isclose(dm, target))\n",
"### END HIDDEN TESTS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Taking the partial trace of a density matrix is the equivalent of taking the marginal of a joint probability distribution along one random variable. If we take the partial trace of this density matrix in any of the qubit subsystems, we are going to get the maximally mixed state, that is, the uniform distribution over the remaining system. This reflects the strong correlation between the two subsystems: if we marginalize over one, we have no predictive power over the other one. We exploit this property when we create protocols for preparing thermal states."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 31e137f

Please sign in to comment.