add variable index normalization
@@ -173,7 +173,30 @@ class TruthTableOracle(Oracle):
if clause:
clauses.append(clause)
expression = Xor(*clauses)
- return expression.to_ast()
+
+ raw_ast = expression.to_ast()
+ idx_mapping = {
+ u: v + 1 for u, v in zip(sorted(expression.usupport), [v.indices[0] for v in sorted(expression.support)])
+ }
+
+ if raw_ast[0] == 'and' or raw_ast[0] == 'or' or raw_ast[0] == 'xor':
+ clauses = []
+ for c in raw_ast[1:]:
+ if c[0] == 'lit':
+ clauses.append(('lit', (idx_mapping[c[1]]) if c[1] > 0 else (-idx_mapping[-c[1]])))
+ elif (c[0] == 'or' or c[0] == 'and') and (raw_ast[0] != c[0]):
+ clause = []
+ for l in c[1:]:
+ clause.append(('lit', (idx_mapping[l[1]]) if l[1] > 0 else (-idx_mapping[-l[1]])))
+ clauses.append((c[0], *clause))
+ else:
+ raise AquaError('Unrecognized logic expression: {}'.format(raw_ast))
+ elif raw_ast[0] == 'const' or raw_ast[0] == 'lit':
+ return raw_ast
+ else:
+ raise AquaError('Unrecognized root expression type: {}.'.format(raw_ast[0]))
+ ast = (raw_ast[0], *clauses)
+ return ast
@property
def variable_register(self):
