def test_to_quantumchannel_kraus(self): """Test to_quantumchannel for Kraus inputs.""" a_0 = np.array([[1, 0], [0, np.sqrt(1 - 0.3)]], dtype=complex) a_1 = np.array([[0, 0], [0, np.sqrt(0.3)]], dtype=complex) b_0 = np.array([[1, 0], [0, np.sqrt(1 - 0.5)]], dtype=complex) b_1 = np.array([[0, 0], [0, np.sqrt(0.5)]], dtype=complex) target = SuperOp(Kraus([a_0, a_1])).tensor(SuperOp(Kraus([b_0, b_1]))) error = QuantumError([a_0, a_1]).tensor(QuantumError([b_0, b_1])) self.assertEqual(target, error.to_quantumchannel())
def old_approximate_quantum_error(error, *, operator_string=None, operator_dict=None, operator_list=None): if not isinstance(error, QuantumError): error = QuantumError(error) if error.number_of_qubits > 2: raise NoiseError( "Only 1-qubit and 2-qubit noises can be converted, {}-qubit " "noise found in model".format(error.number_of_qubits)) error_kraus_operators = Kraus(error.to_quantumchannel()).data transformer = NoiseTransformer() if operator_string is not None: no_info_error = "No information about noise type {}".format( operator_string) operator_string = operator_string.lower() if operator_string not in transformer.named_operators.keys(): raise RuntimeError(no_info_error) operator_lists = transformer.named_operators[operator_string] if len(operator_lists) < error.number_of_qubits: raise RuntimeError( no_info_error + " for {} qubits".format(error.number_of_qubits)) operator_dict = operator_lists[error.number_of_qubits - 1] if operator_dict is not None: _, operator_list = zip(*operator_dict.items()) if operator_list is not None: op_matrix_list = [ transformer.operator_matrix(operator) for operator in operator_list ] probabilities = transformer.transform_by_operator_list( op_matrix_list, error_kraus_operators) identity_prob = numpy.round(1 - sum(probabilities), 9) if identity_prob < 0 or identity_prob > 1: raise RuntimeError( "Channel probabilities sum to {}".format(1 - identity_prob)) quantum_error_spec = [([{ 'name': 'id', 'qubits': [0] }], identity_prob)] op_circuit_list = [ transformer.operator_circuit(operator) for operator in operator_list ] for (operator, probability) in zip(op_circuit_list, probabilities): quantum_error_spec.append((operator, probability)) return QuantumError(quantum_error_spec) raise NoiseError( "Quantum error approximation failed - no approximating operators detected" )
def test_to_quantumchannel_circuit(self): """Test to_quantumchannel for circuit inputs.""" noise_ops = [([{ 'name': 'reset', 'qubits': [0] }], 0.2), ([{ 'name': 'reset', 'qubits': [1] }], 0.3), ([{ 'name': 'id', 'qubits': [0] }], 0.5)] error = QuantumError(noise_ops) reset = SuperOp( np.array([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])) iden = SuperOp(np.eye(4)) target = 0.2 * iden.tensor(reset) + 0.3 * reset.tensor( iden) + 0.5 * iden.tensor(iden) self.assertEqual(target, error.to_quantumchannel())