add `measurement` boolean flag to Shor's `construct_circuit()`
@@ -301,9 +301,13 @@ class Shor(QuantumAlgorithm):
inverse=True
)
- def construct_circuit(self):
+ def construct_circuit(self, measurement=False):
"""Construct circuit.
+ Args:
+ measurement (bool): Boolean flag to indicate if measurement should be included in the circuit.
+
+
Returns:
QuantumCircuit: quantum circuit.
"""
@@ -338,6 +342,11 @@ class Shor(QuantumAlgorithm):
# Apply inverse QFT
ftc.construct_circuit(circuit=circuit, qubits=self._up_qreg, do_swaps=True, inverse=True)
+ if measurement:
+ up_cqreg = ClassicalRegister(2 * self._n, name='m')
+ circuit.add_register(up_cqreg)
+ circuit.measure(self._up_qreg, up_cqreg)
+
logger.info(summarize_circuits(circuit))
return circuit
@@ -445,9 +454,8 @@ class Shor(QuantumAlgorithm):
if not self._ret['factors']:
logger.debug('Running with N={} and a={}.'.format(self._N, self._a))
- circuit = self.construct_circuit()
-
if self._quantum_instance.is_statevector:
+ circuit = self.construct_circuit(measurement=False)
logger.warning('The statevector_simulator might lead to subsequent computation using too much memory.')
result = self._quantum_instance.execute(circuit)
complete_state_vec = result.get_statevector(circuit)
@@ -463,9 +471,7 @@ class Shor(QuantumAlgorithm):
if not v == 0:
counts[bin(int(i))[2:].zfill(2 * self._n)] = v ** 2
else:
- up_cqreg = ClassicalRegister(2 * self._n, name='m')
- circuit.add_register(up_cqreg)
- circuit.measure(self._up_qreg, up_cqreg)
+ circuit = self.construct_circuit(measurement=True)
counts = self._quantum_instance.execute(circuit).get_counts(circuit)
self._ret['results'] = dict()
