|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +namespace Quantum.Kata.JointMeasurements |
| 5 | +{ |
| 6 | + open Microsoft.Quantum.Primitive; |
| 7 | + open Microsoft.Quantum.Canon; |
| 8 | + open Microsoft.Quantum.Extensions.Convert; |
| 9 | + open Microsoft.Quantum.Extensions.Math; |
| 10 | + |
| 11 | + ////////////////////////////////////////////////////////////////// |
| 12 | + // Welcome! |
| 13 | + ////////////////////////////////////////////////////////////////// |
| 14 | + |
| 15 | + // "Joint Measurements" quantum kata is a series of exercises designed |
| 16 | + // to get you familiar with programming in Q#. |
| 17 | + // It covers the joint parity measurements and using them for distinguishing quantum states |
| 18 | + // or for performing multi-qubit gates. |
| 19 | + // |
| 20 | + // Each task is wrapped in one operation preceded by the description of the task. |
| 21 | + // Each task (except tasks in which you have to write a test) has a unit test associated with it, |
| 22 | + // which initially fails. Your goal is to fill in the blank (marked with // ... comment) |
| 23 | + // with some Q# code to make the failing test pass. |
| 24 | + // |
| 25 | + // The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks. |
| 26 | + |
| 27 | + |
| 28 | + // Task 1. Single-qubit measurement |
| 29 | + // Input: Two qubits (stored in an array) which are guaranteed to be |
| 30 | + // either in superposition of states |00⟩ and |11⟩ |
| 31 | + // or in superposition of states |01⟩ and |10⟩. |
| 32 | + // Output: 0 if qubits were in the first superposition, |
| 33 | + // 1 if they were in the second superposition. |
| 34 | + // The state of the qubits at the end of the operation does not matter. |
| 35 | + operation SingleQubitMeasurement (qs : Qubit[]) : Int |
| 36 | + { |
| 37 | + body |
| 38 | + { |
| 39 | + // Hint: Use two single-qubit measurements. |
| 40 | + // ... |
| 41 | + return -1; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Task 2. Parity measurement |
| 46 | + // Input: Two qubits (stored in an array) which are guaranteed to be |
| 47 | + // either in superposition of states |00⟩ and |11⟩ |
| 48 | + // or in superposition of states |01⟩ and |10⟩. |
| 49 | + // Output: 0 if qubits were in the first superposition, |
| 50 | + // 1 if they were in the second superposition. |
| 51 | + // The state of the qubits at the end of the operation should be the same as the starting state. |
| 52 | + operation ParityMeasurement (qs : Qubit[]) : Int |
| 53 | + { |
| 54 | + body |
| 55 | + { |
| 56 | + // ... |
| 57 | + return -1; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Task 3. |0000⟩ + |1111⟩ or |0011⟩ + |1100⟩ ? |
| 62 | + // Input: Four qubits (stored in an array) which are guaranteed to be |
| 63 | + // either in superposition of states |0000⟩ and |1111⟩ |
| 64 | + // or in superposition of states |0011⟩ and |1100⟩. |
| 65 | + // Output: 0 if qubits were in the first superposition, |
| 66 | + // 1 if they were in the second superposition. |
| 67 | + // The state of the qubits at the end of the operation should be the same as the starting state. |
| 68 | + operation GHZOrGHZWithX (qs : Qubit[]) : Int |
| 69 | + { |
| 70 | + body |
| 71 | + { |
| 72 | + // ... |
| 73 | + return -1; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Task 4. |0..0⟩ + |1..1⟩ or W state ? |
| 78 | + // Input: An even number of qubits (stored in an array) which are guaranteed to be |
| 79 | + // either in superposition of states |0..0⟩ and |1..1⟩ |
| 80 | + // or in W state ( https://en.wikipedia.org/wiki/W_state ). |
| 81 | + // Output: 0 if qubits were in W state, |
| 82 | + // 1 if they were in the second superposition. |
| 83 | + // The state of the qubits at the end of the operation should be the same as the starting state. |
| 84 | + operation GHZOrWState (qs : Qubit[]) : Int |
| 85 | + { |
| 86 | + body |
| 87 | + { |
| 88 | + // ... |
| 89 | + return -1; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Task 5. Parity measurement in different basis |
| 94 | + // Input: Two qubits (stored in an array) which are guaranteed to be |
| 95 | + // either in superposition α|00⟩ + β|01⟩ + β|10⟩ + α|11⟩ |
| 96 | + // or in superposition α|00⟩ - β|01⟩ + β|10⟩ - α|11⟩. |
| 97 | + // Output: 0 if qubits were in the first superposition, |
| 98 | + // 1 if they were in the second superposition. |
| 99 | + // The state of the qubits at the end of the operation should be the same as the starting state. |
| 100 | + operation DifferentBasis (qs : Qubit[]) : Int |
| 101 | + { |
| 102 | + body |
| 103 | + { |
| 104 | + // ... |
| 105 | + return -1; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Task 6. Controlled X gate with |0⟩ target |
| 110 | + // Input: Two unentangled qubits (stored in an array of length 2). |
| 111 | + // The first qubit will be in state |ψ⟩ = α |0⟩ + β |1⟩, the second - in state |0⟩ |
| 112 | + // (this can be written as two-qubit state (α|0⟩ + β|1⟩) ⊕ |0⟩). |
| 113 | + // Goal: Change the two-qubit state to α |00⟩ + β |11⟩ using only single-qubit gates and joint measurements. |
| 114 | + // Do not use two-qubit gates. |
| 115 | + // You do not need to allocate extra qubits. |
| 116 | + operation ControlledX (qs : Qubit[]) : () |
| 117 | + { |
| 118 | + body |
| 119 | + { |
| 120 | + // ... |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Task 7*. Controlled X gate with arbitrary target |
| 125 | + // Input: Two qubits (stored in an array of length 2) in an arbitrary |
| 126 | + // two-qubit state α|00⟩ + β|01⟩ + γ|10⟩ + δ|11⟩. |
| 127 | + // Goal: Change the two-qubit state to α|00⟩ + β|01⟩ + δ|10⟩ + γ|11⟩ using only single-qubit gates and joint measurements. |
| 128 | + // Do not use two-qubit gates. |
| 129 | + operation ControlledX_General (qs : Qubit[]) : () |
| 130 | + { |
| 131 | + body |
| 132 | + { |
| 133 | + // Hint: You can use an extra qubit to perform this operation. |
| 134 | + // ... |
| 135 | + } |
| 136 | + adjoint self; |
| 137 | + } |
| 138 | +} |
0 commit comments