Skip to content

Commit

Permalink
Determine the max number of ancillae for all ESOPs (TruthTableOracle …
Browse files Browse the repository at this point in the history
…fix) (qiskit-community#1128)

* determine the max number of ancillae for all ESOPs

* fix for when no ancillae are needed

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
  • Loading branch information
2 people authored and pbark committed Sep 16, 2020
1 parent 6cf8e4d commit f544bbc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
39 changes: 22 additions & 17 deletions qiskit/aqua/circuits/boolean_logical_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ def _set_up_register(num_qubits_needed, provided_register, description):

return None

def compute_num_ancillae(self, mct_mode='basic'):
""" returns the number of ancillary qubits needed """
max_num_ancillae = max(
max(
self._num_clauses if self._clause_register else 0,
self._max_clause_size
) - 2,
0
)
num_ancillae = 0
if mct_mode in ('basic', 'basic-dirty-ancilla'):
num_ancillae = max_num_ancillae
elif mct_mode == 'advanced':
if max_num_ancillae >= 3:
num_ancillae = 1
elif mct_mode == 'noancilla':
pass
else:
raise ValueError('Unsupported MCT mode {}.'.format(mct_mode))
return num_ancillae

def _set_up_circuit(
self,
circuit=None,
Expand All @@ -204,23 +225,7 @@ def _set_up_circuit(
)
self._output_idx = output_idx if output_idx else 0

max_num_ancillae = max(
max(
self._num_clauses if self._clause_register else 0,
self._max_clause_size
) - 2,
0
)
num_ancillae = 0
if mct_mode in ('basic', 'basic-dirty-ancilla'):
num_ancillae = max_num_ancillae
elif mct_mode == 'advanced':
if max_num_ancillae >= 3:
num_ancillae = 1
elif mct_mode == 'noancilla':
pass
else:
raise ValueError('Unsupported MCT mode {}.'.format(mct_mode))
num_ancillae = self.compute_num_ancillae(mct_mode)

self._ancillary_register = BooleanLogicNormalForm._set_up_register(
num_ancillae, ancillary_register, 'ancilla'
Expand Down
7 changes: 7 additions & 0 deletions qiskit/aqua/components/oracles/truth_table_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,18 @@ def construct_circuit(self):
self._circuit = QuantumCircuit()
self._output_register = QuantumRegister(self._num_outputs, name='o')
if self._esops:
num_ancillae, self._ancillary_register = 0, None
for i, e in enumerate(self._esops):
num_ancillae = max(num_ancillae, e.compute_num_ancillae(self._mct_mode))
if num_ancillae > 0:
self._ancillary_register = QuantumRegister(num_ancillae, name='a')

for i, e in enumerate(self._esops):
if e is not None:
ci = e.construct_circuit(
output_register=self._output_register,
output_idx=i,
ancillary_register=self._ancillary_register,
mct_mode=self._mct_mode
)
self._circuit += ci
Expand Down

0 comments on commit f544bbc

Please sign in to comment.