Home Reference Source
import GridMapper from 'projectq/src/cengines/twodmapper.js'
public class | source

GridMapper

Extends:

BasicEngineBasicMapperEngine → GridMapper

Mapper to a 2-D grid graph.

Mapped qubits on the grid are numbered in row-major order. E.g. for 3 rows and 2 columns:

0 - 1
|   |
2 - 3
|   |
4 - 5

The numbers are the mapped qubit ids. The backend might number the qubits on the grid differently (e.g. not row-major), we call these backend qubit ids. If the backend qubit ids are not row-major, one can pass a dictionary translating from our row-major mapped ids to these backend ids.

Note: The algorithm sorts twice inside each column and once inside each row.

Constructor Summary

Public Constructor
public

constructor(args: {num_rows: number, num_columns: number, mapped_ids_to_backend_ids: ?Object, storage: ?number, optimization_function: ?function, num_optimization_steps: ?number})

Member Summary

Public Members
public set
public get
public
public
public

Statistics

public
public
public
public
public
public

storage: *

Private Members
private
private
private
private

Logical qubit ids for which the Allocate gate has already been processed and sent to the next engine but which are not yet deallocated:

private
private

Change between 2D and 1D mappings (2D is a snake like 1D chain) Note it translates to our mapped ids in row major order and not backend ids which might be different.

private
private

Storing commands

Method Summary

Public Methods
public

isAvailable(cmd: *): *

public

receive(command_list: Command[])

Receives a command list and, for each command, stores it until we do a mapping (FlushGate || Cache of stored commands is full).

public

Returns a new mapping of the qubits.

public

returnSwaps(old_mapping: Object, new_mapping: Object, permutation: Array<number[]>): Array<number[]>

Returns the swap operation to change mapping

Private Methods
private

_compareAndSwap(element0: Position, element1: Position, key: function(arg: Position): number): Array<number> | undefined

If swapped (inplace), then return swap operation so that key(element0) < key(element1)

private

_run()

Creates a new mapping and executes possible gates.

private

Sends the stored commands possible without changing the mapping.

private

_sortWithinColumns(final_positions: Array<Position[]>, key: function(arg: Position): number): Array<number[]>

private

_sortWithinRows(final_positions: Array<Position[]>, key: function(arg: Position): number): Array<number[]>

Inherited Summary

From class BasicEngine
public
public

Return a new qubit as a list containing 1 qubit object (quantum register of size 1).

public

Allocate n qubits and return them as a quantum register, which is a list of qubit objects.

public

Deallocate a qubit (and sends the deallocation command down the pipeline).

public

Default implementation of isAvailable: Ask the next engine whether a command is available, i.e., whether it can be executed by the next engine(s).

public

Check if there is a compiler engine handling the meta tag

public
public

send(commandList: Command[])

Forward the list of commands to the next engine in the pipeline.

From class BasicMapperEngine
public get
public set
private
public

Send this Command using the mapped qubit ids of this.current_mapping.

Public Constructors

public constructor(args: {num_rows: number, num_columns: number, mapped_ids_to_backend_ids: ?Object, storage: ?number, optimization_function: ?function, num_optimization_steps: ?number}) source

Override:

BasicMapperEngine#constructor

Params:

NameTypeAttributeDescription
args {num_rows: number, num_columns: number, mapped_ids_to_backend_ids: ?Object, storage: ?number, optimization_function: ?function, num_optimization_steps: ?number}

Throw:

Error

if incorrect mapped_ids_to_backend_ids parameter

Public Members

public set currentMapping source

Override:

BasicMapperEngine#currentMapping

public get currentMapping: * source

Override:

BasicMapperEngine#currentMapping

public depth_of_swaps: {} source

public num_columns: * source

public num_mappings: number source

Statistics

public num_of_swaps_per_mapping: {} source

public num_optimization_steps: * source

public num_qubits: * source

public num_rows: * source

Properties:

NameTypeAttributeDescription
this.num_rows number

Number of rows in the grid

this.num_columns number

Number of columns in the grid.

this.num_qubits number
this._mapped_ids_to_backend_ids Object<number, number>
this._backend_ids_to_mapped_ids Object<number, number>

Stores a mapping from mapped ids which are 0,...,this.num_qubits-1 in row-major order on the grid to the corresponding qubit ids of the backend. Key: mapped id. Value: corresponding backend id. Default is None which means backend ids are identical to mapped ids.

this._current_row_major_mapping Object<number, number>
this.storage number

Number of gates to temporarily store

this.optimization_function function

Function which takes a list of swaps and returns a cost value. Mapper chooses a permutation which minimizes this cost. Default optimizes for circuit depth.

this.num_optimization_steps number

Number of different permutations to of the matching to try and minimize the cost.

this.num_mappings number
this.depth_of_swaps Object<number, number>
this.num_of_swaps_per_mapping Object<number, number>

public optimization_function: * source

public storage: * source

Private Members

private _backend_ids_to_mapped_ids: {} source

private _currentMapping: * source

Override:

BasicMapperEngine#_currentMapping

private _current_row_major_mapping: * source

private _currently_allocated_ids: * source

Logical qubit ids for which the Allocate gate has already been processed and sent to the next engine but which are not yet deallocated:

Properties:

NameTypeAttributeDescription
this._currently_allocated_ids Set<number>

private _map_1d_to_2d: {} source

private _map_2d_to_1d: {} source

Change between 2D and 1D mappings (2D is a snake like 1D chain) Note it translates to our mapped ids in row major order and not backend ids which might be different.

Properties:

NameTypeAttributeDescription
this._map_2d_to_1d Object<number, number>
this._map_1d_to_2d Object<number, number>

private _mapped_ids_to_backend_ids: * source

private _stored_commands: *[] source

Storing commands

Properties:

NameTypeAttributeDescription
this._stored_commands Command[]

Public Methods

public isAvailable(cmd: *): * source

Default implementation of isAvailable: Ask the next engine whether a command is available, i.e., whether it can be executed by the next engine(s).

Override:

BasicEngine#isAvailable

Params:

NameTypeAttributeDescription
cmd *

Return:

*

public receive(command_list: Command[]) source

Receives a command list and, for each command, stores it until we do a mapping (FlushGate || Cache of stored commands is full).

Override:

BasicEngine#receive

Params:

NameTypeAttributeDescription
command_list Command[]

list of commands to receive.

public returnNewMapping(): Object source

Returns a new mapping of the qubits.

It goes through this._saved_commands and tries to find a mapping to apply these gates on a first come first served basis. It reuses the function of a 1D mapper and creates a mapping for a 1D linear chain and then wraps it like a snake onto the square grid.

One might create better mappings by specializing this function for a square grid.

Return:

Object

A new mapping as a dict. key is logical qubit id, value is mapped id

public returnSwaps(old_mapping: Object, new_mapping: Object, permutation: Array<number[]>): Array<number[]> source

Returns the swap operation to change mapping

Params:

NameTypeAttributeDescription
old_mapping Object

dict keys are logical ids and values are mapped qubit ids

new_mapping Object

dict keys are logical ids and values are mapped qubit ids

permutation Array<number[]>

list of int from 0, 1, ..., this.num_rows-1. It is used to permute the found perfect matchings. Default is None which keeps the original order.

Return:

Array<number[]>

List of tuples. Each tuple is a swap operation which needs to be applied. Tuple contains the two mapped qubit ids for the Swap.

Private Methods

private _compareAndSwap(element0: Position, element1: Position, key: function(arg: Position): number): Array<number> | undefined source

If swapped (inplace), then return swap operation so that key(element0) < key(element1)

Params:

NameTypeAttributeDescription
element0 Position
element1 Position
key function(arg: Position): number

Return:

Array<number> | undefined

private _run() source

Creates a new mapping and executes possible gates.

It first allocates all 0, ..., this.num_qubits-1 mapped qubit ids, if they are not already used because we might need them all for the swaps. Then it creates a new map, swaps all the qubits to the new map, executes all possible gates, and finally deallocates mapped qubit ids which don't store any information.

private _sendPossibleCommands() source

Sends the stored commands possible without changing the mapping. Note: this._current_row_major_mapping (hence also this.currentMapping) must exist already

private _sortWithinColumns(final_positions: Array<Position[]>, key: function(arg: Position): number): Array<number[]> source

Params:

NameTypeAttributeDescription
final_positions Array<Position[]>
key function(arg: Position): number

Return:

Array<number[]>

private _sortWithinRows(final_positions: Array<Position[]>, key: function(arg: Position): number): Array<number[]> source

Params:

NameTypeAttributeDescription
final_positions Array<Position[]>
key function(arg: Position): number

Return:

Array<number[]>