ibm

The IBM Setup imports the default IBM decomposition rules, compiler engines (featuring a CNOTMapper), and the IBM backend (IBMBackend).

Module contents

Registers a variety of useful gate decompositions, specifically for the IBM quantum experience backend. Among others it includes:

  • Controlled z-rotations –> Controlled NOTs and single-qubit rotations
  • Toffoli gate –> CNOT and single-qubit gates
  • m-Controlled global phases –> (m-1)-controlled phase-shifts
  • Global phases –> ignore
  • (controlled) Swap gates –> CNOTs and Toffolis
class projectq.setups.ibm.AutoReplacer(decomposition_chooser=<function AutoReplacer.<lambda>>)

The AutoReplacer is a compiler engine which uses engine.is_available in order to determine which commands need to be replaced/decomposed/compiled further. The loaded setup is used to find decomposition rules appropriate for each command (e.g., setups.default).

__init__(decomposition_chooser=<function AutoReplacer.<lambda>>)

Initialize an AutoReplacer.

Parameters:decomposition_chooser (function) – A function which, given the Command to decompose and a list of potential Decomposition objects, determines (and then returns) the ‘best’ decomposition.

The default decomposition chooser simply returns the first list element, i.e., calling

repl = AutoReplacer()

Amounts to

def decomposition_chooser(cmd, decomp_list):
        return decomp_list[0]
repl = AutoReplacer(decomposition_chooser)
receive(command_list)

Receive a list of commands from the previous compiler engine and, if necessary, replace/decompose the gates according to the decomposition rules in the loaded setup.

Parameters:command_list (list<Command>) – List of commands to handle.
class projectq.setups.ibm.IBMBackend(use_hardware=False, num_runs=1024, verbose=False, user=None, password=None)

The IBM Backend class, which stores the circuit, transforms it to JSON QASM, and sends the circuit through the IBM API.

__init__(use_hardware=False, num_runs=1024, verbose=False, user=None, password=None)

Initialize the Backend object.

Parameters:
  • use_hardware (bool) – If True, the code is run on the IBM quantum chip (instead of using the IBM simulator)
  • num_runs (int) – Number of runs to collect statistics. (default is 1024)
  • verbose (bool) – If True, statistics are printed, in addition to the measurement result being registered (at the end of the circuit).
  • user (string) – IBM Quantum Experience user name
  • password (string) – IBM Quantum Experience password
get_probabilities(qureg)

Return the list of basis states with corresponding probabilities.

The measured bits are ordered according to the supplied quantum register, i.e., the left-most bit in the state-string corresponds to the first qubit in the supplied quantum register.

Warning

Only call this function after the circuit has been executed!

Parameters:qureg (list<Qubit>) – Quantum register determining the order of the qubits.
Returns:Dictionary mapping n-bit strings to probabilities.
Return type:probability_dict (dict)
Raises:Exception – If no data is available (i.e., if the circuit has not been executed).
is_available(cmd)

Return true if the command can be executed.

The IBM quantum chip can do X, Y, Z, T, Tdag, S, Sdag, and CX / CNOT.

Parameters:cmd (Command) – Command for which to check availability
receive(command_list)

Receives a command list and, for each command, stores it until completion.

Parameters:command_list – List of commands to execute
class projectq.setups.ibm.IBMCNOTMapper

CNOT mapper for the IBM backend.

Transforms CNOTs such that all CNOTs within the circuit have the same target qubit (required by IBM backend). If necessary, it will flip around the CNOT gate by first applying Hadamard gates to both qubits, then CNOT with swapped control and target qubit, and finally Hadamard gates to both qubits.

Note

The mapper has to be run once on the entire circuit. Else, an Exception will be raised (if, e.g., several measurements are performed without re- initializing the mapper).

Warning

If the provided circuit cannot be mapped to the hardware layout without performing Swaps, the mapping procedure raises an Exception.

__init__()

Initialize an IBM CNOT Mapper compiler engine.

Resets the mapping.

is_available(cmd)

Check if the IBM backend can perform the Command cmd and return True if so.

Parameters:cmd (Command) – The command to check
receive(command_list)

Receives a command list and, for each command, stores it until completion.

Parameters:command_list (list of Command objects) – list of commands to receive.
Raises:Exception – If mapping the CNOT gates to 1 qubit would require Swaps. The current version only supports remapping of CNOT gates without performing any Swaps due to the large costs associated with Swapping given the CNOT constraints.
class projectq.setups.ibm.LocalOptimizer(m=5)

LocalOptimizer is a compiler engine which optimizes locally (merging rotations, cancelling gates with their inverse) in a local window of user- defined size.

It stores all commands in a list of lists, where each qubit has its own gate pipeline. After adding a gate, it tries to merge / cancel successive gates using the get_merged and get_inverse functions of the gate (if available). For examples, see BasicRotationGate. Once a list corresponding to a qubit contains >=m gates, the pipeline is sent on to the next engine.

__init__(m=5)

Initialize a LocalOptimizer object.

Parameters:m (int) – Number of gates to cache per qubit, before sending on the first gate.
receive(command_list)

Receive commands from the previous engine and cache them. If a flush gate arrives, the entire buffer is sent on.

class projectq.setups.ibm.TagRemover(tags=[<class 'projectq.meta._compute.ComputeTag'>, <class 'projectq.meta._compute.UncomputeTag'>])

TagRemover is a compiler engine which removes temporary command tags (see the tag classes such as LoopTag in projectq.meta._loop).

Removing tags is important (after having handled them if necessary) in order to enable optimizations across meta-function boundaries (compute/action/ uncompute or loops after unrolling)

__init__(tags=[<class 'projectq.meta._compute.ComputeTag'>, <class 'projectq.meta._compute.UncomputeTag'>])

Construct the TagRemover.

Parameters:tags – A list of meta tag classes (e.g., [ComputeTag, UncomputeTag]) denoting the tags to remove
receive(command_list)

Receive a list of commands from the previous engine, remove all tags which are an instance of at least one of the meta tags provided in the constructor, and then send them on to the next compiler engine.

Parameters:command_list (list<Command>) – List of commands to receive and then (after removing tags) send on.