Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Determine the max number of ancillae for all ESOPs (TruthTableOracle fix) #1128

Merged
merged 3 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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