deltakit.circuit.Circuit.replace_gates#
- Circuit.replace_gates(replacement_policy: Mapping[type[I | X | Y | Z | H | S | S_DAG | SQRT_X | SQRT_X_DAG | SQRT_Y | SQRT_Y_DAG | H_XY | H_YZ | C_XYZ | C_ZYX | CX | CXSWAP | CY | CZ | CZSWAP | ISWAP | ISWAP_DAG | SQRT_XX | SQRT_XX_DAG | SQRT_YY | SQRT_YY_DAG | SQRT_ZZ | SQRT_ZZ_DAG | SWAP | XCX | XCY | XCZ | YCX | YCY | YCZ | RZ | RX | RY | MX | MY | MZ | MRX | MRY | MRZ | HERALD_LEAKAGE_EVENT | MPP] | I | X | Y | Z | H | S | S_DAG | SQRT_X | SQRT_X_DAG | SQRT_Y | SQRT_Y_DAG | H_XY | H_YZ | C_XYZ | C_ZYX | CX | CXSWAP | CY | CZ | CZSWAP | ISWAP | ISWAP_DAG | SQRT_XX | SQRT_XX_DAG | SQRT_YY | SQRT_YY_DAG | SQRT_ZZ | SQRT_ZZ_DAG | SWAP | XCX | XCY | XCZ | YCX | YCY | YCZ | RZ | RX | RY | MX | MY | MZ | MRX | MRY | MRZ | HERALD_LEAKAGE_EVENT | MPP, Callable[[I | X | Y | Z | H | S | S_DAG | SQRT_X | SQRT_X_DAG | SQRT_Y | SQRT_Y_DAG | H_XY | H_YZ | C_XYZ | C_ZYX | CX | CXSWAP | CY | CZ | CZSWAP | ISWAP | ISWAP_DAG | SQRT_XX | SQRT_XX_DAG | SQRT_YY | SQRT_YY_DAG | SQRT_ZZ | SQRT_ZZ_DAG | SWAP | XCX | XCY | XCZ | YCX | YCY | YCZ | RZ | RX | RY | MX | MY | MZ | MRX | MRY | MRZ | HERALD_LEAKAGE_EVENT | MPP], I | X | Y | Z | H | S | S_DAG | SQRT_X | SQRT_X_DAG | SQRT_Y | SQRT_Y_DAG | H_XY | H_YZ | C_XYZ | C_ZYX | CX | CXSWAP | CY | CZ | CZSWAP | ISWAP | ISWAP_DAG | SQRT_XX | SQRT_XX_DAG | SQRT_YY | SQRT_YY_DAG | SQRT_ZZ | SQRT_ZZ_DAG | SWAP | XCX | XCY | XCZ | YCX | YCY | YCZ | RZ | RX | RY | MX | MY | MZ | MRX | MRY | MRZ | HERALD_LEAKAGE_EVENT | MPP]], recursive: bool = True)#
Replace gates in the circuit with other gates. This can either be replacing all gates of the same type or all particular instances of a gate. In particular this can be used to apply measurement noise to the measurement gates by specifying the keys in the replacement policy to be measurement gates.
Replacing a measurement gate with a non measurement gate or a non measurement gate with a measurement gate will lead to unexpected behaviour
- Parameters:
replacement_policy (GateReplacementPolicy) – A mapping of gates to function which generates a new gate. Every time the gate key is found in a gate layer the gate in the gate layer is replaced with the new generated one. The keys can either be types of gate, in which case all gates of that type will be replaced or it can be an instance of a gate and then all gates which are equal to that gate will be replaced.
recursive (bool, optional) – Whether to apply the replacement policy to nested circuits, by default True.
Examples
>>> import deltakit_circuit as sp >>> circuit = sp.Circuit(sp.GateLayer(sp.gates.MX(0))) >>> circuit.replace_gates( ... {sp.gates.MX: lambda gate: sp.gates.MX(gate.qubit, 0.01)} ... ) >>> circuit.gate_layers [GateLayer([ MX(Qubit(0), probability=0.01) ])]You can also use this to manipulate the non-zero probabilities of existing measurement gates.
>>> circuit = sp.Circuit(sp.GateLayer(sp.gates.MZ(0, 0.001))) >>> circuit.replace_gates( ... { ... sp.gates.MZ: lambda gate: sp.gates.MZ( ... gate.qubit, gate.probability * 10 ... ) ... } ... ) >>> circuit.gate_layers [GateLayer([ MZ(Qubit(0), probability=0.01) ])]