This commit is contained in:
Daniel Tsvetkov 2020-02-03 16:24:32 +01:00
parent 4c4f6bd43b
commit 11ed1b44ac
1 changed files with 16 additions and 2 deletions

View File

@ -231,6 +231,18 @@ class State(Vector):
return "{:0" + str(int(np.ceil(np.log2(len(self))))) + "b}"
def measure(self):
"""
Measures in the computational basis
TODO: Generalize the method so it takes a basis
TODO: Should we memoize the result? Should we memoize per basis?
If it's measured twice, should it return the same state?
What about if measured twice but in different bases?
E.g. measure1 -> computation -> A
measure2 -> +/- basis -> B
measure3 -> computation -> should it return A again or random weighted?
measure4 -> +/- basis -> should it return B again or random weighted?
:return: binary representation of the measured qubit (e.g. "011")
"""
weights = [self.get_prob(j) for j in range(len(self))]
format_str = self.get_fmt_of_element()
choices = [format_str.format(i) for i in range(len(weights))]
@ -239,13 +251,15 @@ class State(Vector):
def measure_with_op(self, mo):
"""
Measures with a measurement operator mo
TODO: Can't define mo: MeasurementOperator because in python you can't declare classes before defining them
TODO: Can't define `mo: MeasurementOperator` because in python you can't declare classes before defining them
"""
m = mo.on(self) / np.sqrt(mo.get_prob(self))
return State(m)
def measure_n(self, n=1):
"""measures n times"""
"""measures n times
TODO: Does this make sense? When a state is measured once, it should "collapse"
"""
measurements = defaultdict(int)
for _ in range(n):
k = self.measure()