Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Add statistical test for magic rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
garrison committed Jun 17, 2022
1 parent 1ff6afc commit 0ed7382
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/test_magic_rounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,45 @@ def test_noisy_quantuminstance():
magic.round(RoundingContext(trace_values=[1, 1, 1], var2op=var2op, circuit=circuit))


def test_magic_rounding_statistical():
"""Statistical test for magic rounding
to make sure each deterministic case rounds consistently
"""
shots = 1024
mr = MagicRounding(
QuantumInstance(backend=Aer.get_backend("qasm_simulator"), shots=shots)
)

test_cases = (
# 3 variables encoded as a (3,1,p) QRAC
((0, 0, 0), 0, 0),
((1, 1, 1), 0, 1),
((0, 1, 1), 1, 0),
((1, 0, 0), 1, 1),
((1, 0, 1), 2, 0),
((0, 1, 0), 2, 1),
((1, 1, 0), 3, 0),
((0, 0, 1), 3, 1),
# 2 variables encoded as a (3,1,p) QRAC (see issue #11)
((0, 0), 0, 0),
((1, 1), 3, 0),
((0, 1), 2, 1),
((1, 0), 1, 1),
# 1 variable encoded as a (3,1,p) QRAC (see issue #11)
((0,), 0, 0),
((1,), 1, 1),
)

for (m, basis, expected) in test_cases:
encoding = QuantumRandomAccessEncoding()
encoding._add_variables(list(range(len(m))))
circuit = encoding.state_prep(m).to_circuit()
result = mr.round(RoundingContext(encoding=encoding, circuit=circuit))
deterministic_dict = result.basis_counts[basis]
assert set(deterministic_dict) == set([str(expected)])


if __name__ == "__main__":
import sys

Expand Down

1 comment on commit 0ed7382

@garrison
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments should have actually referenced #7, not #11. But they will not be around for long, as they'll no longer be necessary once #33 is merged.

Please sign in to comment.