quantumlib / OpenFermion

The electronic structure package for quantum computers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`prepare_slater_determinant` doesn't flip bits as expected

lmarti-dev opened this issue · comments

I hope this is the right place to ask this question.

I am trying to implement an algorithm which prepares a Slater determinant as described in arXiv:1711.05395.

The prepare_slater_determinant expects an initial_state which should be the "computational basis state that the qubits start in".

However, this piece of code seems to defeat that purpose as it doesn't correctly flip the bits, but rather will flip bits if their index is lower than the number of rows in the Q matrix (exclusive) or they are in initial_state (here rewritten as initially_occupied_orbitals).

    yield (cirq.X(qubits[j])
           for j in range(n_qubits)
           if (j < n_occupied) != (j in initially_occupied_orbitals))

This means, for example, that

initially_occupied_orbitals = [2,3]
n_qubits = 4
n_occupied = 2

will put Xs on qubits 0,1,2,3.

Is this intended?

This is intended. The algorithm starts out by setting the first N_f qubits to 1 and the rest to 0, where N_f is the number of particles. In your example, since there are 4 orbitals and 2 particles, the algorithm will add X gates to make the starting state 1100.

Thanks for the answer.

I feel like the purpose of initial_state still eludes me, though.

For example, if I run this simple code

import openfermion as of
import numpy as np
import cirq

N=4
Nf=2
Q = np.random.randn(N,N)
initial_state=[2,3]

qubits = [cirq.LineQubit(j) for j in range(N)]
circuit=cirq.Circuit()
circuit+=of.prepare_slater_determinant(slater_determinant_matrix=Q[:Nf,:],qubits=qubits,initial_state=initial_state)

print(circuit)

I get

0: ───X─────────────────────────PhISwap(0.25)─────────────────────────────────────────────────────────────
                                │
1: ───X───PhISwap(0.25)─────────PhISwap(0.25)^-0.419───Z^0───────────────────PhISwap(0.25)────────────────
          │                                                                  │
2: ───X───PhISwap(0.25)^0.766───Z^0────────────────────PhISwap(0.25)─────────PhISwap(0.25)^-0.183───Z^0───
                                                       │
3: ───X────────────────────────────────────────────────PhISwap(0.25)^0.712───Z^0──────────────────────────

whereas, naively, I would expect

0: ─────────────────────────────PhISwap(0.25)─────────────────────────────────────────────────────────────
                                │
1: ───────PhISwap(0.25)─────────PhISwap(0.25)^-0.419───Z^0───────────────────PhISwap(0.25)────────────────
          │                                                                  │
2: ───X───PhISwap(0.25)^0.766───Z^0────────────────────PhISwap(0.25)─────────PhISwap(0.25)^-0.183───Z^0───
                                                       │
3: ───X────────────────────────────────────────────────PhISwap(0.25)^0.712───Z^0──────────────────────────

Is there a subtlety of initial_state that I am missing?

Apologies for the closing and reopening, this was unintended

whereas, naively, I would expect

Why do you expect this?

Just to clarify, initial_state refers to the state of the qubits before the circuit is applied.

The documentation for prepare_slater_determinant mentions

For example, the list [2, 3] represents qubits 2 and 3 being set to one

Which I would expect results in the circuit being setup in such a way that qubits 2 and 3 are set to one.

Just to clarify, initial_state refers to the state of the qubits before the circuit is applied.

If I understand correctly, this means initial_state informs the function of the computational state that will be inputed?

If I understand correctly, this means initial_state informs the function of the computational state that will be inputed?

Yes, it refers to the state before the gates of the circuit get applied (including the X gates)

Which I would expect results in the circuit being setup in such a way that qubits 2 and 3 are set to one.

The circuit assumes qubits 2 and 3 are set to one, so it is flipping them to set them to zero, and adding X gates to set qubits 0 and 1 to one, which is required for the rest of the circuit.

Thank you for your answer and you time. I will close this.