Example #1
0
    def test_trotterization(self):

        circ_vec = [Circuit(), build_circuit('Z_0')]
        coef_vec = [-1.0j * 0.5, -1.0j * -0.04544288414432624]

        # the operator to be exponentiated
        minus_iH = QubitOperator()
        for i in range(len(circ_vec)):
            minus_iH.add(coef_vec[i], circ_vec[i])

        # exponentiate the operator
        Utrot, phase = trotterization.trotterize(minus_iH)

        inital_state = np.zeros(2**4, dtype=complex)
        inital_state[3] = np.sqrt(2 / 3)
        inital_state[12] = -np.sqrt(1 / 3)

        # initalize a quantum computer with above coeficients
        # i.e. ca|1100> + cb|0011>
        qc = Computer(4)
        qc.set_coeff_vec(inital_state)

        # apply the troterized minus_iH
        qc.apply_circuit(Utrot)
        qc.apply_constant(phase)

        smart_print(qc)

        coeffs = qc.get_coeff_vec()

        assert np.real(coeffs[3]) == approx(0.6980209737879599, abs=1.0e-15)
        assert np.imag(coeffs[3]) == approx(-0.423595782342996, abs=1.0e-15)
        assert np.real(coeffs[12]) == approx(-0.5187235657531178, abs=1.0e-15)
        assert np.imag(coeffs[12]) == approx(0.25349397560041553, abs=1.0e-15)
Example #2
0
    def test_build_from_openfermion(self):
        print('\n')
        trial_state = qforte.QuantumComputer(4)

        trial_prep = [None] * 5
        trial_prep[0] = qforte.gate('H', 0, 0)
        trial_prep[1] = qforte.gate('H', 1, 1)
        trial_prep[2] = qforte.gate('H', 2, 2)
        trial_prep[3] = qforte.gate('H', 3, 3)
        trial_prep[4] = qforte.gate('cX', 0, 1)

        trial_circ = qforte.QuantumCircuit()

        #prepare the circuit
        for gate in trial_prep:
            trial_circ.add_gate(gate)

        # use circuit to prepare trial state
        trial_state.apply_circuit(trial_circ)

        test_operator = QubitOperator('X2 Y1', 0.0 - 0.25j)
        test_operator += QubitOperator('Y2 Y1', 0.25)
        test_operator += QubitOperator('X2 X1', 0.25)
        test_operator += QubitOperator('Y2 X1', 0.0 + 0.25j)
        print(test_operator)

        qforte_operator = qforte.build_from_openfermion(test_operator)

        qforte.smart_print(qforte_operator)

        exp = trial_state.direct_op_exp_val(qforte_operator)
        print(exp)
        self.assertAlmostEqual(exp, 0.2499999999999999 + 0.0j)
Example #3
0
    def test_trotterization_with_controlled_U(self):

        circ_vec = [build_circuit('Y_0 X_1'), build_circuit('X_0 Y_1')]
        coef_vec = [-1.0719145972781818j, 1.0719145972781818j]

        # the operator to be exponentiated
        minus_iH = QubitOperator()
        for i in range(len(circ_vec)):
            minus_iH.add(coef_vec[i], circ_vec[i])

        ancilla_idx = 2

        # exponentiate the operator
        Utrot, phase = trotterization.trotterize_w_cRz(minus_iH, ancilla_idx)

        # Case 1: positive control

        # initalize a quantum computer
        qc = Computer(3)

        # build HF state
        qc.apply_gate(gate('X', 0, 0))

        # put ancilla in |1> state
        qc.apply_gate(gate('X', 2, 2))

        # apply the troterized minus_iH
        qc.apply_circuit(Utrot)

        smart_print(qc)

        coeffs = qc.get_coeff_vec()

        assert coeffs[5] == approx(-0.5421829373021542, abs=1.0e-15)
        assert coeffs[6] == approx(-0.8402604730072732, abs=1.0e-15)

        # Case 2: negitive control

        # initalize a quantum computer
        qc = Computer(3)

        # build HF state
        qc.apply_gate(gate('X', 0, 0))

        # apply the troterized minus_iH
        qc.apply_circuit(Utrot)

        smart_print(qc)

        coeffs = qc.get_coeff_vec()

        assert coeffs[1] == approx(1, abs=1.0e-15)
Example #4
0
    def do_qite_step(self):

        btot = self.build_b()
        A = qf.QubitOperator()

        if(self._sparseSb):
            sp_idxs, S, btot = self.build_sparse_S_b(btot)
        else:
            S = self.build_S()

        x = lstsq(S, btot)[0]
        x = np.real(x)


        # sing_vals = lstsq(S, btot)[3]
        # print(np.abs(sing_vals[-1] / sing_vals[0]))

        if(self._sparseSb):
            for I, spI in enumerate(sp_idxs):
                if np.abs(x[I]) > self._x_thresh:
                    A.add(-1.0j * self._db * x[I], self._sig.terms()[spI][1].terms()[0][1])
                    self._n_classical_params += 1

        else:
            for I, SigI in enumerate(self._sig.terms()):
                if np.abs(x[I]) > self._x_thresh:
                    A.add(-1.0j * self._db * x[I], SigI[1].terms()[0][1])
                    self._n_classical_params += 1

        if(self._verbose):
            print('\nbtot:\n ', btot)
            print('\n S:  \n')
            matprint(S)
            print('\n x:  \n')
            print(x)

        eiA_kb, phase1 = trotterize(A, trotter_number=self._trotter_number)
        self._total_phase *= phase1
        self._Uqite.add(eiA_kb)
        self._qc.apply_circuit(eiA_kb)
        self._Ekb.append(np.real(self._qc.direct_op_exp_val(self._qb_ham)))

        self._n_cnot += eiA_kb.get_num_cnots()

        if(self._verbose):
            qf.smart_print(self._qc)
    def test_circ_op_print(self):

        # initialize empty circuit
        circ1 = Circuit()
        # add (Z1 H2 Y4 X4) Pauli string
        # note: the rightmost gate is applied first
        circ1.add(gate('X', 4))
        circ1.add(gate('Y', 4))
        circ1.add(gate('H', 2))
        circ1.add(gate('Z', 1))

        # test built-in print function
        out = str(circ1)
        assert out == '[Z1 H2 Y4 X4]'

        # test smart_print function
        with patch('sys.stdout', new=StringIO()) as fake_out:
            smart_print(circ1)
            assert fake_out.getvalue(
            ) == '\n Quantum circuit:\n(Z1 H2 Y4 X4) |Ψ>\n'

        # initialize empty circuit
        circ2 = Circuit()
        # add (H1 Y2 S3 I4) Pauli string
        # note: the rightmost gate is applied first
        circ2.add(gate('I', 4))
        circ2.add(gate('S', 3))
        circ2.add(gate('Y', 2))
        circ2.add(gate('H', 1))

        # initialize empty circuit
        circ3 = Circuit()
        # add (X1 Y2 H3 Z4) Pauli string
        # note: the rightmost gate is applied first
        circ3.add(gate('Z', 4))
        circ3.add(gate('H', 3))
        circ3.add(gate('Y', 2))
        circ3.add(gate('X', 1))

        # initialize empty QubitOperator
        q_op = QubitOperator()

        # add u1*circ1 + u2*circ2 + u3*circ3
        u1 = 0.5 + 0.1j
        u2 = -0.5j
        u3 = +0.3

        q_op.add(u1, circ1)
        q_op.add(u2, circ2)
        q_op.add(u3, circ3)

        # test built-in print function
        out = str(q_op)
        assert out == '+0.500000 +0.100000i[Z1 H2 Y4 X4]\n'\
                         '-0.500000j[H1 Y2 S3 I4]\n'\
                         '+0.300000[X1 Y2 H3 Z4]'

        # test smart_print function
        with patch('sys.stdout', new=StringIO()) as fake_out:
            smart_print(q_op)
            assert fake_out.getvalue() == '\n Quantum operator:\n(0.5+0.1j) (Z1 H2 Y4 X4) |Ψ>\n'\
                             '+ (-0-0.5j) (H1 Y2 S3 I4) |Ψ>\n'\
                             '+ (0.3+0j) (X1 Y2 H3 Z4) |Ψ>\n'