def _construct_diffusion_circuit(): num_variable_qubits = n var_reg2 = QuantumRegister(num_variable_qubits) qc = AncillaCircuit(var_reg2) qc.h(var_reg2) qc.u(np.pi, 0, np.pi, var_reg2) qc.u(np.pi / 2, 0, np.pi, var_reg2[-1]) qc.mcx(var_reg2[:-1], var_reg2[-1]) qc.u(np.pi / 2, 0, np.pi, var_reg2[-1]) qc.u(np.pi, 0, np.pi, var_reg2) qc.h(var_reg2) return qc
def GroverQpp( n, oracle ): # oracle should be an ancilla gate on n + 1 qb, result in last qb _num_iterations = int(np.floor(np.pi / 4.0 * np.sqrt(pow(2, n)))) def _construct_diffusion_circuit(): num_variable_qubits = n var_reg2 = QuantumRegister(num_variable_qubits) qc = AncillaCircuit(var_reg2) qc.h(var_reg2) qc.u(np.pi, 0, np.pi, var_reg2) qc.u(np.pi / 2, 0, np.pi, var_reg2[-1]) qc.mcx(var_reg2[:-1], var_reg2[-1]) qc.u(np.pi / 2, 0, np.pi, var_reg2[-1]) qc.u(np.pi, 0, np.pi, var_reg2) qc.h(var_reg2) return qc def qc_amplitude_amplification_iteration(): reg = QuantumRegister(n + 1, name='reg') _qc_aa_iteration = AncillaCircuit(QuantumRegister(n + 1)) _qc_aa_iteration.append(oracle, reg) _qc_aa_iteration.append( _construct_diffusion_circuit().to_ancilla_gate(), reg[:-1]) return _qc_aa_iteration var_reg = QuantumRegister(n, name='var_reg') out_reg = QuantumRegister(1, name='out_reg') qc = AncillaCircuit(var_reg, out_reg) qc.u(np.pi, 0, np.pi, out_reg) # x qc.u(np.pi / 2, 0, np.pi, out_reg) # h qc.h(var_reg) for _ in range(_num_iterations): qc.append(qc_amplitude_amplification_iteration().to_ancilla_gate(), [*var_reg, out_reg]) return (qc, var_reg)