def test_init_multiple_digits(self): """Test __init__ for sparse label with multiple digits""" actual = SpinOp([("X_10^20", 1 + 2j), ("X_12^34", 56)], Fraction(5, 2), register_length=13) desired = [("X_10^20", 1 + 2j), ("X_12^34", 56)] self.assertListEqual(actual.to_list(), desired)
def test_init_label(self, label, pre_processing): """Test __init__""" spin = SpinOp(pre_processing(label), register_length=len(label) // 3) expected_label = " ".join(lb for lb in label.split() if lb[0] != "I") if not expected_label: expected_label = f"I_{len(label) // 3 - 1}" self.assertListEqual(spin.to_list(), [(expected_label, 1)]) self.assertSpinEqual(eval(repr(spin)), spin) # pylint: disable=eval-used
def map(self, second_q_op: SpinOp) -> PauliSumOp: """Map spins to qubits using the Logarithmic encoding. Args: second_q_op: Spins mapped to qubits. Returns: Qubit operators generated by the Logarithmic encoding """ qubit_ops_list: List[PauliSumOp] = [] # get logarithmic encoding of the general spin matrices. spinx, spiny, spinz, identity = self._logarithmic_encoding( second_q_op.spin) for idx, (_, coeff) in enumerate(second_q_op.to_list()): operatorlist: List[PauliSumOp] = [] for n_x, n_y, n_z in zip(second_q_op.x[idx], second_q_op.y[idx], second_q_op.z[idx]): operator_on_spin_i: List[PauliSumOp] = [] if n_x > 0: operator_on_spin_i.append( reduce(operator.matmul, [spinx] * int(n_x))) if n_y > 0: operator_on_spin_i.append( reduce(operator.matmul, [spiny] * int(n_y))) if n_z > 0: operator_on_spin_i.append( reduce(operator.matmul, [spinz] * int(n_z))) if operator_on_spin_i: single_operator_on_spin_i = reduce(operator.matmul, operator_on_spin_i) operatorlist.append(single_operator_on_spin_i) else: # If n_x=n_y=n_z=0, simply add the embedded Identity operator. operatorlist.append(identity) # Now, we can tensor all operators in this list qubit_ops_list.append(coeff * reduce(operator.xor, reversed(operatorlist))) qubit_op = reduce(operator.add, qubit_ops_list) return qubit_op
def map(self, second_q_op: SpinOp) -> PauliSumOp: qubit_ops_list: List[PauliSumOp] = [] # get linear encoding of the general spin matrices spinx, spiny, spinz, identity = self._linear_encoding(second_q_op.spin) for idx, (_, coeff) in enumerate(second_q_op.to_list()): operatorlist: List[PauliSumOp] = [] for n_x, n_y, n_z in zip(second_q_op.x[idx], second_q_op.y[idx], second_q_op.z[idx]): operator_on_spin_i: List[PauliSumOp] = [] if n_x > 0: operator_on_spin_i.append( reduce(operator.matmul, [spinx] * int(n_x))) if n_y > 0: operator_on_spin_i.append( reduce(operator.matmul, [spiny] * int(n_y))) if n_z > 0: operator_on_spin_i.append( reduce(operator.matmul, [spinz] * int(n_z))) if np.any([n_x, n_y, n_z]) > 0: single_operator_on_spin_i = reduce(operator.matmul, operator_on_spin_i) operatorlist.append(single_operator_on_spin_i.reduce()) else: # If n_x=n_y=n_z=0, simply add the embedded Identity operator. operatorlist.append(identity) # Now, we can tensor all operators in this list # NOTE: in Qiskit's opflow the `XOR` (i.e. `^`) operator does the tensor product qubit_ops_list.append(coeff * reduce(operator.xor, reversed(operatorlist))) qubit_op = reduce(operator.add, qubit_ops_list) return qubit_op