def test_qae_circuit(self, efficient_circuit, use_circuit_library): """Test circuits resulting from canonical amplitude estimation. Build the circuit manually and from the algorithm and compare the resulting unitaries. """ if not use_circuit_library: # ignore deprecation warnings from QFTs warnings.filterwarnings(action="ignore", category=DeprecationWarning) prob = 0.5 for m in range(2, 7): qae = AmplitudeEstimation(m, a_factory=BernoulliAFactory(prob)) angle = 2 * np.arcsin(np.sqrt(prob)) # manually set up the inefficient AE circuit q_ancilla = QuantumRegister(m, 'a') q_objective = QuantumRegister(1, 'q') circuit = QuantumCircuit(q_ancilla, q_objective) # initial Hadamard gates for i in range(m): circuit.h(q_ancilla[i]) # A operator circuit.ry(angle, q_objective) if efficient_circuit: qae.q_factory = BernoulliQFactory(qae.a_factory) for power in range(m): circuit.cry(2 * 2 ** power * angle, q_ancilla[power], q_objective[0]) else: q_factory = QFactory(qae.a_factory, i_objective=0) for power in range(m): for _ in range(2**power): q_factory.build_controlled(circuit, q_objective, q_ancilla[power]) # fourier transform if use_circuit_library: iqft = QFT(m, do_swaps=False).inverse() circuit.append(iqft.to_instruction(), q_ancilla) else: iqft = Standard(m) iqft.construct_circuit(qubits=q_ancilla, circuit=circuit, do_swaps=False) expected_unitary = self._unitary.execute(circuit).get_unitary() actual_circuit = qae.construct_circuit(measurement=False) actual_unitary = self._unitary.execute(actual_circuit).get_unitary() diff = np.sum(np.abs(actual_unitary - expected_unitary)) self.assertAlmostEqual(diff, 0) if not use_circuit_library: warnings.filterwarnings(action="always", category=DeprecationWarning)
def test_qae_circuit(self, efficient_circuit): """Test circuits resulting from canonical amplitude estimation. Build the circuit manually and from the algorithm and compare the resulting unitaries. """ prob = 0.5 problem = EstimationProblem(BernoulliStateIn(prob), objective_qubits=[0]) for m in [2, 5]: qae = AmplitudeEstimation(m) angle = 2 * np.arcsin(np.sqrt(prob)) # manually set up the inefficient AE circuit qr_eval = QuantumRegister(m, "a") qr_objective = QuantumRegister(1, "q") circuit = QuantumCircuit(qr_eval, qr_objective) # initial Hadamard gates for i in range(m): circuit.h(qr_eval[i]) # A operator circuit.ry(angle, qr_objective) if efficient_circuit: qae.grover_operator = BernoulliGrover(prob) for power in range(m): circuit.cry(2 * 2**power * angle, qr_eval[power], qr_objective[0]) else: oracle = QuantumCircuit(1) oracle.z(0) state_preparation = QuantumCircuit(1) state_preparation.ry(angle, 0) grover_op = GroverOperator(oracle, state_preparation) grover_op.global_phase = np.pi for power in range(m): circuit.compose( grover_op.power(2**power).control(), qubits=[qr_eval[power], qr_objective[0]], inplace=True, ) # fourier transform iqft = QFT(m, do_swaps=False).inverse().reverse_bits() circuit.append(iqft.to_instruction(), qr_eval) actual_circuit = qae.construct_circuit(problem, measurement=False) self.assertEqual(Operator(circuit), Operator(actual_circuit))
def construct_circuit(circuit=None, qubits=None, inverse=False, approximation_degree=0, do_swaps=True): """Construct the circuit representing the desired state vector. Args: circuit (QuantumCircuit): The optional circuit to extend from. qubits (Union(QuantumRegister, list[Qubit])): The optional qubits to construct the circuit with. approximation_degree (int): degree of approximation for the desired circuit inverse (bool): Boolean flag to indicate Inverse Quantum Fourier Transform do_swaps (bool): Boolean flag to specify if swaps should be included to align the qubit order of input and output. The output qubits would be in reversed order without the swaps. Returns: QuantumCircuit: quantum circuit Raises: AquaError: invalid input """ warnings.warn( 'The class FourierTransformCircuits is deprecated and will be removed ' 'no earlier than 3 months after the release 0.7.0. You should use the ' 'qiskit.circuit.library.QFT class instead.', DeprecationWarning, stacklevel=2) if circuit is None: raise AquaError('Missing input QuantumCircuit.') if qubits is None: raise AquaError('Missing input qubits.') qft = QFT(len(qubits), approximation_degree=approximation_degree, do_swaps=do_swaps) if inverse: qft = qft.inverse() circuit.append(qft.to_instruction(), qubits) return circuit