Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
madcpf committed Nov 10, 2023
1 parent ed330bc commit dfd50cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
7 changes: 3 additions & 4 deletions unitary/alpha/quantum_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def get_histogram(
histogram[idx][cast(int, result[idx])] += 1
return histogram

def get_histogram_with_all_objects_as_key(
def get_correlated_histogram(
self, objects: Optional[Sequence[QuantumObject]] = None, count: int = 100
) -> Dict[Tuple[int], int]:
"""Creates histogram of the whole quantum world (or `objects` if specified)
Expand Down Expand Up @@ -576,8 +576,7 @@ def get_binary_probabilities_from_state_vector(
if objects is None:
objects = self.public_objects

simulator = cirq.Simulator()
result = simulator.simulate(self.circuit, initial_state=0)
result = cirq.Simulator().simulate(self.circuit, initial_state=0)
state_vector = result.state_vector()
# qubit_map gives the mapping from object to its index in (the dirac notation of)
# the state vector.
Expand Down Expand Up @@ -633,7 +632,7 @@ def get_binary_probabilities_from_state_vector(
# Renormalize.
final_world_distribution[key] = abs(coefficients[index]) ** 2 / prob_sum

# TODO(pengfeichen): make the above calculation a seperate function, which calculates
# TODO(pengfeichen): make the above calculation a separate function, which calculates
# the distribution of the whole world (or specified objects) by state vector.

# Calculate the distribution of all objects.
Expand Down
12 changes: 6 additions & 6 deletions unitary/alpha/quantum_world_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def test_get_histogram_and_get_probabilities_one_binary_qobject(
)
histogram = world.get_histogram()
assert histogram == [{0: 0, 1: 100}]
histogram = world.get_histogram_with_all_objects_as_key()
histogram = world.get_correlated_histogram()
assert histogram == {(1,): 100}
probs = world.get_probabilities()
assert probs == [{0: 0.0, 1: 1.0}]
Expand All @@ -667,7 +667,7 @@ def test_get_histogram_and_get_probabilities_one_binary_qobject(
alpha.Flip()(l1)
histogram = world.get_histogram()
assert histogram == [{0: 100, 1: 0}]
histogram = world.get_histogram_with_all_objects_as_key()
histogram = world.get_correlated_histogram()
assert histogram == {(0,): 100}
probs = world.get_probabilities()
assert probs == [{0: 1.0, 1: 0.0}]
Expand All @@ -681,7 +681,7 @@ def test_get_histogram_and_get_probabilities_one_binary_qobject(
assert len(histogram[0]) == 2
assert histogram[0][0] > 10
assert histogram[0][1] > 10
histogram = world.get_histogram_with_all_objects_as_key()
histogram = world.get_correlated_histogram()
assert len(histogram) == 2
assert histogram[(0,)] > 10
assert histogram[(1,)] > 10
Expand Down Expand Up @@ -714,7 +714,7 @@ def test_get_histogram_and_get_probabilities_one_trinary_qobject(
)
histogram = world.get_histogram()
assert histogram == [{0: 0, 1: 100, 2: 0}]
histogram = world.get_histogram_with_all_objects_as_key()
histogram = world.get_correlated_histogram()
assert histogram == {(1,): 100}
probs = world.get_probabilities()
assert probs == [{0: 0.0, 1: 1.0, 2: 0.0}]
Expand Down Expand Up @@ -746,15 +746,15 @@ def test_get_histogram_and_get_probabilities_two_qobjects(simulator, compile_to_
)
histogram = world.get_histogram()
assert histogram == [{0: 0, 1: 100}, {0: 0, 1: 100, 2: 0}]
histogram = world.get_histogram_with_all_objects_as_key()
histogram = world.get_correlated_histogram()
assert histogram == {(1, 1): 100}
probs = world.get_probabilities()
assert probs == [{0: 0.0, 1: 1.0}, {0: 0.0, 1: 1.0, 2: 0.0}]
bin_probs = world.get_binary_probabilities()
assert bin_probs == [1.0, 1.0]
histogram = world.get_histogram(objects=[l2], count=1000)
assert histogram == [{0: 0, 1: 1000, 2: 0}]
histogram = world.get_histogram_with_all_objects_as_key(objects=[l2], count=1000)
histogram = world.get_correlated_histogram(objects=[l2], count=1000)
assert histogram == {(1,): 1000}
probs = world.get_probabilities(objects=[l2], count=1000)
assert probs == [{0: 0.0, 1: 1.0, 2: 0.0}]
Expand Down

0 comments on commit dfd50cd

Please sign in to comment.