meta

Contains meta statements which allow more optimal code while making it easier for users to write their code. Examples are with Compute, followed by an automatic uncompute or with Control, which allows the user to condition an entire code block upon the state of a qubit.

projectq.meta.DirtyQubitTag Dirty qubit meta tag
projectq.meta.LogicalQubitIDTag(logical_qubit_id) LogicalQubitIDTag for a mapped qubit to annotate a MeasureGate.
projectq.meta.LoopTag(num) Loop meta tag
projectq.meta.Loop(engine, num) Loop n times over an entire code block.
projectq.meta.Compute(engine) Start a compute-section.
projectq.meta.Uncompute(engine) Uncompute automatically.
projectq.meta.CustomUncompute(engine) Start a custom uncompute-section.
projectq.meta.ComputeTag Compute meta tag.
projectq.meta.UncomputeTag Uncompute meta tag.
projectq.meta.Control(engine, qubits) Condition an entire code block on the value of qubits being 1.
projectq.meta.get_control_count(cmd) Return the number of control qubits of the command object cmd
projectq.meta.Dagger(engine) Invert an entire code block.
projectq.meta.insert_engine(prev_engine, …) Inserts an engine into the singly-linked list of engines.
projectq.meta.drop_engine_after(prev_engine) Removes an engine from the singly-linked list of engines.

Module contents

The projectq.meta package features meta instructions which help both the user and the compiler in writing/producing efficient code. It includes, e.g.,

  • Loop (with Loop(eng): …)
  • Compute/Uncompute (with Compute(eng): …, […], Uncompute(eng))
  • Control (with Control(eng, ctrl_qubits): …)
  • Dagger (with Dagger(eng): …)
class projectq.meta.Compute(engine)[source]

Start a compute-section.

Example

with Compute(eng):
    do_something(qubits)
action(qubits)
Uncompute(eng) # runs inverse of the compute section

Warning

If qubits are allocated within the compute section, they must either be uncomputed and deallocated within that section or, alternatively, uncomputed and deallocated in the following uncompute section.

This means that the following examples are valid:

with Compute(eng):
    anc = eng.allocate_qubit()
    do_something_with_ancilla(anc)
    ...
    uncompute_ancilla(anc)
    del anc

do_something_else(qubits)

Uncompute(eng)  # will allocate a new ancilla (with a different id)
                # and then deallocate it again
with Compute(eng):
    anc = eng.allocate_qubit()
    do_something_with_ancilla(anc)
    ...

do_something_else(qubits)

Uncompute(eng)  # will deallocate the ancilla!

After the uncompute section, ancilla qubits allocated within the compute section will be invalid (and deallocated). The same holds when using CustomUncompute.

Failure to comply with these rules results in an exception being thrown.

__init__(engine)[source]

Initialize a Compute context.

Parameters:engine (BasicEngine) – Engine which is the first to receive all commands (normally: MainEngine).
class projectq.meta.ComputeTag[source]

Compute meta tag.

class projectq.meta.Control(engine, qubits)[source]

Condition an entire code block on the value of qubits being 1.

Example

with Control(eng, ctrlqubits):
    do_something(otherqubits)
__init__(engine, qubits)[source]

Enter a controlled section.

Parameters:
  • engine – Engine which handles the commands (usually MainEngine)
  • qubits (list of Qubit objects) – Qubits to condition on

Enter the section using a with-statement:

with Control(eng, ctrlqubits):
    ...
class projectq.meta.CustomUncompute(engine)[source]

Start a custom uncompute-section.

Example

with Compute(eng):
    do_something(qubits)
action(qubits)
with CustomUncompute(eng):
    do_something_inverse(qubits)
Raises:QubitManagementError – If qubits are allocated within Compute or within CustomUncompute context but are not deallocated.
__init__(engine)[source]

Initialize a CustomUncompute context.

Parameters:engine (BasicEngine) – Engine which is the first to receive all commands (normally: MainEngine).
class projectq.meta.Dagger(engine)[source]

Invert an entire code block.

Use it with a with-statement, i.e.,

with Dagger(eng):
    [code to invert]

Warning

If the code to invert contains allocation of qubits, those qubits have to be deleted prior to exiting the ‘with Dagger()’ context.

This code is NOT VALID:

with Dagger(eng):
    qb = eng.allocate_qubit()
    H | qb # qb is still available!!!

The correct way of handling qubit (de-)allocation is as follows:

with Dagger(eng):
    qb = eng.allocate_qubit()
    ...
    del qb # sends deallocate gate (which becomes an allocate)
__init__(engine)[source]

Enter an inverted section.

Parameters:engine – Engine which handles the commands (usually MainEngine)

Example (executes an inverse QFT):

with Dagger(eng):
    QFT | qubits
class projectq.meta.DirtyQubitTag[source]

Dirty qubit meta tag

class projectq.meta.LogicalQubitIDTag(logical_qubit_id)[source]

LogicalQubitIDTag for a mapped qubit to annotate a MeasureGate.

logical_qubit_id

int – Logical qubit id

__init__(logical_qubit_id)[source]

Initialize self. See help(type(self)) for accurate signature.

class projectq.meta.Loop(engine, num)[source]

Loop n times over an entire code block.

Example

with Loop(eng, 4):
    # [quantum gates to be executed 4 times]

Warning

If the code in the loop contains allocation of qubits, those qubits have to be deleted prior to exiting the ‘with Loop()’ context.

This code is NOT VALID:

with Loop(eng, 4):
    qb = eng.allocate_qubit()
    H | qb # qb is still available!!!

The correct way of handling qubit (de-)allocation is as follows:

with Loop(eng, 4):
    qb = eng.allocate_qubit()
    ...
    del qb # sends deallocate gate
__init__(engine, num)[source]

Enter a looped section.

Parameters:
  • engine – Engine handling the commands (usually MainEngine)
  • num (int) – Number of loop iterations

Example

with Loop(eng, 4):
    H | qb
    Rz(M_PI/3.) | qb
Raises:
  • TypeError – If number of iterations (num) is not an integer
  • ValueError – If number of iterations (num) is not >= 0
class projectq.meta.LoopTag(num)[source]

Loop meta tag

__init__(num)[source]

Initialize self. See help(type(self)) for accurate signature.

loop_tag_id = 0[source]
projectq.meta.Uncompute(engine)[source]

Uncompute automatically.

Example

with Compute(eng):
    do_something(qubits)
action(qubits)
Uncompute(eng) # runs inverse of the compute section
class projectq.meta.UncomputeTag[source]

Uncompute meta tag.

projectq.meta.drop_engine_after(prev_engine)[source]

Removes an engine from the singly-linked list of engines.

Parameters:prev_engine (projectq.cengines.BasicEngine) – The engine just before the engine to drop.
Returns:The dropped engine.
Return type:Engine
projectq.meta.get_control_count(cmd)[source]

Return the number of control qubits of the command object cmd

projectq.meta.insert_engine(prev_engine, engine_to_insert)[source]

Inserts an engine into the singly-linked list of engines.

It also sets the correct main_engine for engine_to_insert.

Parameters: