apply_op_to

apply_op_to(op1, op1_qargs, op2, op2_qargs, *, apply_as_transform=False)[source]

Apply the operator op2 to the operator op1.

These operators do not necessarily need to act on the same number of qubits, as they are assumed to act on a larger system. The position in the system of each operator is defined by the corresponding qargs. The output operator will be defined on union(op1_qargs, op2_qargs).

By default, this function applies op1 to op2 in the following way:

op2 @ op1

By setting apply_as_transform=True, this function will apply op1 to op2 in the following way:

op2.adjoint() @ op1 @ op2

Parameters:
  • op1 (SparsePauliOp) – The operator on which op2 will be applied.

  • op1_qargs (list[int]) – The qubit indices for op1.

  • op2 (SparsePauliOp) – The operator to apply to op1.

  • op2_qargs (list[int]) – The qubit indices for op2.

  • apply_as_transform (bool) – Whether to apply op2 to op1 as a transformation.

Returns:

The tuple (op, qargs) where op is the input op1 with op2 left-applied and qargs is a list of qubit indices for the new operator op. The qubit IDs in the output op correspond to the global qubit ID in the same index in qargs.

For example, if the output op is a SparsePauliOp("YX") and qargs is [3, 4], the X term on qubit 0 of the operator corresponds to global qubit ID 3.

Raises:

ValueError – The number of unique operator qargs must match the number of qubits in the corresponding operator.

Return type:

tuple[SparsePauliOp, list[int]]