qml.registers

registers(register_dict)[source]

Returns a dictionary that maps register names to Wires.

This function helps to group qubits and abstract away the finer details of running quantum algorithms. Register names and their total number of wires are typically known in advance, but managing the specific wire range for each register can be a challenge. The qml.registers() function creates a dictionary that maps register names to Wires objects. Moreover, it allows one to input a nested structure where registers contain sub-registers, as illustrated in the examples below.

Parameters

register_dict (dict) – a dictionary where keys are register names and values are either positive integers indicating the number of qubits or nested dictionaries of more registers

Returns

Dictionary where the keys are the names (str) of the registers, and the values are Wires objects.

Return type

dict

Example

Given flat input dictionary:

>>> qml.registers({"alice": 2, "bob": 3})
{'alice': Wires([0, 1]), 'bob': Wires([2, 3, 4])}

Given nested input dictionary:

>>> wire_registers = qml.registers({"people": {"alice": 2, "bob": 1}})
>>> wire_registers
{'alice': Wires([0, 1]), 'bob': Wires([2]), 'people': Wires([0, 1, 2])}
>>> wire_registers['bob']
Wires([2])
>>> wire_registers['alice'][1]
1

A simple example showcasing how to implement the SWAP test:

dev = qml.device("default.qubit")
reg = qml.registers({"aux": 1, "phi": 5, "psi": 5})

@qml.qnode(dev)
def circuit():
    for state in ["phi", "psi"]:
         qml.BasisState([1, 1, 0, 0, 0], reg[state])

    qml.Hadamard(reg["aux"])
    for i in range(len(reg["phi"])):
        qml.CSWAP(reg["aux"] + reg["phi"][i] + reg["psi"][i])
    qml.Hadamard(reg["aux"])

    return qml.expval(qml.Z(wires=reg["aux"]))
>>> circuit()
tensor(1., requires_grad=True)

Contents

Using PennyLane

Release news

Development

API

Internals