-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurement.py
62 lines (46 loc) · 1.49 KB
/
measurement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 17 12:35:02 2021
@author: VED DHARKAR
"""
#! /usr/bin/python3
import pennylane as qml
from pennylane import numpy as np
import sys
def simple_circuits_20(angle):
"""The code you write for this challenge should be completely contained within this function
between the # QHACK # comment markers.
In this function:
* Rotate the qubit around the x-axis by angle
* Measure the probability the qubit is in the zero state
Args:
angle (float): how much to rotate a state around the x-axis
Returns:
float: the probability of of the state being in the 0 ground state
"""
prob = 0.0
# QHACK #
# Step 1 : initalize a device
num_wires = 2
dev = qml.device('default.qubit', wires=num_wires)
# Step 2 : Create a quantum circuit and qnode
@qml.qnode(dev)
def cir(param):
qml.RX(angle, wires=0)
return qml.probs(wires=0)
# Step 3 : Run the qnode
prob1 = cir(0.1)
prob = prob1[0]
# QHACK #
return prob
if __name__ == "__main__":
# DO NOT MODIFY anything in this code block
# Load and process input
angle_str = sys.stdin.read()
angle = float(angle_str)
ans = simple_circuits_20(angle)
if isinstance(ans, np.tensor):
ans = ans.item()
if not isinstance(ans, float):
raise TypeError("the simple_circuits_20 function needs to return a float")
print(ans)