Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StatePreparation() on individual qubits instead of on a qureg? #277

Closed
cgogolin opened this issue Oct 18, 2018 · 3 comments
Closed

StatePreparation() on individual qubits instead of on a qureg? #277

cgogolin opened this issue Oct 18, 2018 · 3 comments

Comments

@cgogolin
Copy link
Contributor

I know that I can use StatePreparation() on a qureg as follows:

import projectq as pq
from projectq.ops import Measure, StatePreparation
from projectq import MainEngine

backend = pq.backends.Simulator()
eng = pq.MainEngine(backend)

qureg = eng.allocate_qureg(2)
StatePreparation([0,0,0,1]) | qureg
Measure | qureg[0]
Measure | qureg[1]
eng.flush()
print("Measured: {}".format((int(qureg[0]), int(qureg[1]))))

This works and outputs Measured: (1, 1) as expected.

How can I use StatePreparation() when the qubits were allocated individually and not as a qureg?

Suppose have two qubits that were allocated as follows

q0 = eng.allocate_qubit()
q1 = eng.allocate_qubit()

and I can't change that part of the code, but want to prepare them in a state with StatePreparation(). I have tried all of the following, but to no avail:

  • StatePreparation([0,0,0,1]) | [q0, q1] -> gives AttributeError: 'Qureg' object has no attribute 'id'

  • StatePreparation([0,0,0,1]) | (q0, q1) -> gives AssertionError: raised in: ' File "/home/cgogolin/.local/lib/python3.5/site-packages/projectq-0.4.1-py3.5-linux-x86_64.egg/projectq/setups/decompositions/stateprep2cnot.py", line 33, in _decompose_state_preparation' ' assert len(cmd.qubits) == 1'

  • StatePreparation([0,0,0,1]) | Qureg([q0, q1]) -> gives AttributeError: 'Qureg' object has no attribute 'id'

It does (seem to work) if I do the following:

import projectq as pq
from projectq.ops import Measure, StatePreparation
from projectq import MainEngine
from projectq.types import Qureg

backend = pq.backends.Simulator()
eng = pq.MainEngine(backend)

q0 = eng.allocate_qubit()
q1 = eng.allocate_qubit()
qureg = Qureg([q0, q1])
q0.id = 0
q1.id = 1
StatePreparation([0,0,1,0]) | qureg
Measure | q0
Measure | q1
eng.flush()
print("Measured: {}".format((int(q0), int(q1))))

But this can't be the right way of using StatePreparation()? Why do the individually allocated qubits not get an id? It doesn't seem to matter whether I put q0.id = 0 or q0.id = 54. Is that intentional/expected?

@damiansteiger
Copy link
Contributor

  • A quantum register Qureg is a list of Qubit objects.
  • allocate_qubit() returns a quantum register (list) with one qubit, while allocate_qureg(2) returns a list with 2 qubits

In a program one can combine lists of quantum registers (using +) or creating new lists of qubit objects:

q0 = eng.allocate_qubit() # q0 == [Qubit0]
q1 = eng.allocate_qubit() # q1 == [Qubit1]

StatePreparation(...) | q0 + q1  #  as q0 + q1 == [Qubit0, Qubit1]
# or
StatePreparation(...) | [q0[0] , q1[0]]  #  as [q0[0] , q1[0]] == [Qubit0, Qubit1]

PS:
Instead of

Measure | qureg[0]
Measure | qureg[1]

one could do:

from projectq.ops import All
All(Measure) | qureg

and for more quantum registers use the trick above to build a new larger quantum register:

from projectq.ops import All
All(Measure) | qureg + qb0 + qb1

@cgogolin
Copy link
Contributor Author

Thanks for the clarification! Is that documented somewhere? I knew that a Qureg is a list of Qubits, but wasn't aware that allocate_qubit() returns not an individual Qubit, but a Qureg that contains a single Qubit. The comment that can be fond in many of the examples in the docs:

# allocate one qubit
q1 = eng.allocate_qubit()

seems to suggest that q1 ends up being a Qubit not a Qureg...

Is the behavior of allocate_qubit() documented somewhere and I was just too stupid to find it in the docs?

@damiansteiger
Copy link
Contributor

You are welcome, I'm happy to help out.

Yes, all our functions are documented:
allocate_qubit documentation

To find, e.g., MainEngine.allocate_qubit:
Go to MainEngine doc which in this case does not contain allocate_qubit, hence check the parent class BasicEngine (either know the parent class or quickly look it up using the link to the source code) which has it documented. Or just search for it in the docs.

# allocate one qubit does not say anything about the container this qubit is in. One could write
# return a list with one new Qubit object to be more precise. It depends for who the comment is intended for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants