make simon test compute ground-truth
# limitations under the License.
# =============================================================================
-import unittest
+import math
+import numpy as np
from parameterized import parameterized
+import unittest
from qiskit.aqua.components.oracles import SimonOracle
from qiskit.aqua.algorithms import Simon
-from qiskit.aqua import get_aer_backend
+from qiskit.aqua import get_aer_backend, AquaError
from test.common import QiskitAquaTestCase
@@ -33,11 +35,25 @@ class TestSimon(QiskitAquaTestCase):
'100': '000', '101': '110', '110': '101', '111': '010'}]
])
def test_simon(self, simon_input):
+ # find the two keys that have matching values
+ nbits = math.log(len(simon_input), 2)
+ if math.ceil(nbits) != math.floor(nbits):
+ raise AquaError('Input not the right length')
+ nbits = int(nbits)
+ get_key_pair = ((k1, k2) for k1, v1 in simon_input.items()
+ for k2, v2 in simon_input.items()
+ if v1 == v2 and not k1 == k2)
+ try: # matching keys found
+ k1, k2 = next(get_key_pair)
+ hidden = np.binary_repr(int(k1, 2) ^ int(k2, 2), nbits)
+ except StopIteration as e: # non matching keys found
+ k1, k2 = None, None
+ hidden = np.binary_repr(0, nbits)
backend = get_aer_backend('qasm_simulator')
oracle = SimonOracle(simon_input)
algorithm = Simon(oracle)
result = algorithm.run(backend)
- self.assertTrue(result['oracle_evaluation'])
+ self.assertEqual(result['result'], hidden)
if __name__ == '__main__':
