Example #1
0
    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

        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
            iqft = Standard(m)
            circuit = 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)
Example #2
0
    def test_mlae_circuits(self, efficient_circuit):
        """ Test the circuits constructed for MLAE """
        prob = 0.5

        for k in range(1, 7):
            qae = MaximumLikelihoodAmplitudeEstimation(
                k, a_factory=BernoulliAFactory(prob))
            angle = 2 * np.arcsin(np.sqrt(prob))

            # compute all the circuits used for MLAE
            circuits = []

            # 0th power
            q_objective = QuantumRegister(1, 'q')
            circuit = QuantumCircuit(q_objective)
            circuit.ry(angle, q_objective)
            circuits += [circuit]

            # powers of 2
            for power in range(k):
                q_objective = QuantumRegister(1, 'q')
                circuit = QuantumCircuit(q_objective)

                # A operator
                circuit.ry(angle, q_objective)

                # Q^(2^j) operator
                if efficient_circuit:
                    qae.q_factory = BernoulliQFactory(qae.a_factory)
                    circuit.ry(2 * 2**power * angle, q_objective[0])

                else:
                    q_factory = QFactory(qae.a_factory, i_objective=0)
                    for _ in range(2**power):
                        q_factory.build(circuit, q_objective)

            actual_circuits = qae.construct_circuits(measurement=False)

            for actual, expected in zip(actual_circuits, circuits):
                expected_unitary = self._unitary.execute(
                    expected).get_unitary()
                actual_unitary = self._unitary.execute(actual).get_unitary()
                diff = np.sum(np.abs(actual_unitary - expected_unitary))
                self.assertAlmostEqual(diff, 0)
Example #3
0
    def setUp(self):
        super().setUp()
        self.a_bernoulli = BernoulliAFactory(0)
        self.q_bernoulli = BernoulliQFactory(self.a_bernoulli)
        self.i_bernoulli = 0

        num_qubits = 5
        self.a_integral = SineIntegralAFactory(num_qubits)
        self.q_intergal = QFactory(self.a_integral, num_qubits)
        self.i_intergal = num_qubits
Example #4
0
    def test_iqae_circuits(self, efficient_circuit):
        """Test circuits resulting from iterative amplitude estimation.

        Build the circuit manually and from the algorithm and compare the resulting unitaries.
        """
        prob = 0.5

        for k in range(2, 7):
            qae = IterativeAmplitudeEstimation(
                0.01, 0.05, a_factory=BernoulliAFactory(prob))
            angle = 2 * np.arcsin(np.sqrt(prob))

            # manually set up the inefficient AE circuit
            q_objective = QuantumRegister(1, 'q')
            circuit = QuantumCircuit(q_objective)

            # A operator
            circuit.ry(angle, q_objective)

            if efficient_circuit:
                qae.q_factory = BernoulliQFactory(qae.a_factory)
                # for power in range(k):
                #    circuit.ry(2 ** power * angle, q_objective[0])
                circuit.ry(2 * k * angle, q_objective[0])

            else:
                q_factory = QFactory(qae.a_factory, i_objective=0)
                for _ in range(k):
                    q_factory.build(circuit, q_objective)

            expected_unitary = self._unitary.execute(circuit).get_unitary()

            actual_circuit = qae.construct_circuit(k, measurement=False)
            actual_unitary = self._unitary.execute(
                actual_circuit).get_unitary()

            diff = np.sum(np.abs(actual_unitary - expected_unitary))
            self.assertAlmostEqual(diff, 0)