diff --git a/qiskit/aqua/circuits/boolean_logical_circuits.py b/qiskit/aqua/circuits/boolean_logical_circuits.py index 4812db9d53..216e3c20e0 100644 --- a/qiskit/aqua/circuits/boolean_logical_circuits.py +++ b/qiskit/aqua/circuits/boolean_logical_circuits.py @@ -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, @@ -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' diff --git a/qiskit/aqua/components/oracles/truth_table_oracle.py b/qiskit/aqua/components/oracles/truth_table_oracle.py index 04c4790728..214030d3b0 100644 --- a/qiskit/aqua/components/oracles/truth_table_oracle.py +++ b/qiskit/aqua/components/oracles/truth_table_oracle.py @@ -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