def test_pauli_error_1q_unitary_from_string(self): """Test single-qubit pauli error as unitary qobj from string label""" paulis = ['I', 'X', 'Y', 'Z'] probs = [0.4, 0.3, 0.2, 0.1] error = pauli_error(zip(paulis, probs), standard_gates=False) target_unitaries = [ standard_gate_unitary('x'), standard_gate_unitary('y'), standard_gate_unitary('z') ] target_probs = probs.copy() target_identity_count = 0 for j in range(len(paulis)): circ, p = error.error_term(j) name = circ[0]['name'] self.assertIn(name, ('unitary', 'id')) self.assertEqual(circ[0]['qubits'], [0]) self.remove_if_found(p, target_probs) if name == "unitary": self.remove_if_found(circ[0]['params'][0], target_unitaries) else: target_identity_count += 1 self.assertEqual(target_probs, [], msg="Incorrect probabilities") self.assertEqual(target_unitaries, [], msg="Incorrect unitaries") self.assertEqual(target_identity_count, 1, msg="Incorrect identities")
def test_depolarizing_error_2q_unitary(self): """Test 2-qubit depolarizing error as unitary qobj""" p_depol = 0.3 error = depolarizing_error(p_depol, 2, standard_gates=False) X = standard_gate_unitary('x') Y = standard_gate_unitary('y') Z = standard_gate_unitary('z') target_unitaries = [X, Y, Z, # on qubit 0 X, Y, Z, # on qubit 1 np.kron(X, X), np.kron(X, Y), np.kron(X, Z), np.kron(Y, X), np.kron(Y, Y), np.kron(Y, Z), np.kron(Z, X), np.kron(Z, Y), np.kron(Z, Z)] for j in range(16): circ, p = error.error_term(j) name = circ[0]['name'] self.assertIn(name, ('unitary', "id")) if name == "unitary": self.assertAlmostEqual(p, p_depol / 16) op = circ[0]['params'] qubits = circ[0]['qubits'] if len(op) == 2: self.assertIn(qubits, [[0], [1]]) else: self.assertEqual(qubits, [0, 1]) self.remove_if_found(op, target_unitaries) else: self.assertAlmostEqual(p, 1 - p_depol + p_depol / 16) self.assertEqual(circ[0]['qubits'], [0]) self.assertEqual(target_unitaries, [], msg="Incorrect unitaries")
def test_equal(self): """Test two quantum errors are equal""" a_i = np.sqrt(0.25) * standard_gate_unitary('id') a_x = np.sqrt(0.25) * standard_gate_unitary('x') a_y = np.sqrt(0.25) * standard_gate_unitary('y') a_z = np.sqrt(0.25) * standard_gate_unitary('z') error1 = QuantumError([a_i, a_x, a_y, a_z], standard_gates=True) error2 = QuantumError([a_i, a_x, a_y, a_z], standard_gates=False) self.assertEqual(error1, error2)
def test_equal(self): """Test two quantum errors are equal""" Ai = np.sqrt(0.25) * standard_gate_unitary('id') Ax = np.sqrt(0.25) * standard_gate_unitary('x') Ay = np.sqrt(0.25) * standard_gate_unitary('y') Az = np.sqrt(0.25) * standard_gate_unitary('z') error1 = QuantumError([Ai, Ax, Ay, Az], standard_gates=True) error2 = QuantumError([Ai, Ax, Ay, Az], standard_gates=False) self.assertEqual(error1, error2)
def setUp(self): self.ops = { 'X': standard_gate_unitary('x'), 'Y': standard_gate_unitary('y'), 'Z': standard_gate_unitary('z'), 'H': standard_gate_unitary('h'), 'S': standard_gate_unitary('s') } self.n = NoiseTransformer()
def test_dot_both_qobj(self): """Test dot of two circuit errors""" unitaries0 = [standard_gate_unitary('id'), standard_gate_unitary('z')] probs0 = [0.9, 0.1] unitaries1 = [standard_gate_unitary('x'), standard_gate_unitary('y')] probs1 = [0.6, 0.4] error0 = QuantumError([ np.sqrt(probs0[0]) * unitaries0[0], np.sqrt(probs0[1]) * unitaries0[1] ], standard_gates=True) error1 = QuantumError([ np.sqrt(probs1[0]) * unitaries1[0], np.sqrt(probs1[1]) * unitaries1[1] ], standard_gates=True) error = error1.dot(error0) # Kronecker product probabilities target_probs = [ probs1[0] * probs0[0], probs1[0] * probs0[1], probs1[1] * probs0[0], probs1[1] * probs0[1] ] # Target circuits target_circs = [[{ 'name': 'x', 'qubits': [0] }], [{ 'name': 'y', 'qubits': [0] }], [{ 'name': 'z', 'qubits': [0] }, { 'name': 'x', 'qubits': [0] }], [{ 'name': 'z', 'qubits': [0] }, { 'name': 'y', 'qubits': [0] }]] for j in range(4): circ, prob = error.error_term(j) # Remove prob from target if it is found # later we will check that target_probs is empty so all # the required ones have been removed self.remove_if_found(prob, target_probs) self.remove_if_found(circ, target_circs) # Check we had all the correct target probs and unitaries # by seeing if these lists are empty # Note that this doesn't actually check that the correct # prob was assigned to the correct unitary. self.assertEqual(target_probs, [], msg="Incorrect compose probabilities") self.assertEqual(target_circs, [], msg="Incorrect compose circuits")
def test_equal(self): """Test two quantum errors are equal""" with self.assertWarns( DeprecationWarning, msg=r"standard_gate_unitary is deprecated as of qiskit-aer 0\.10\.0.*", ): a_i = np.sqrt(0.25) * standard_gate_unitary('id') a_x = np.sqrt(0.25) * standard_gate_unitary('x') a_y = np.sqrt(0.25) * standard_gate_unitary('y') a_z = np.sqrt(0.25) * standard_gate_unitary('z') with self.assertKrausWarning(): error1 = QuantumError([a_i, a_x, a_y, a_z], standard_gates=True) error2 = QuantumError([a_i, a_x, a_y, a_z], standard_gates=False) self.assertEqual(error1, error2)
def test_pauli_conversion_standard_gates(self): """Test conversion of Pauli channel kraus to gates""" Ai = np.sqrt(0.25) * standard_gate_unitary('id') Ax = np.sqrt(0.25) * standard_gate_unitary('x') Ay = np.sqrt(0.25) * standard_gate_unitary('y') Az = np.sqrt(0.25) * standard_gate_unitary('z') error_dict = QuantumError([Ai, Ax, Ay, Az], standard_gates=True).as_dict() self.assertEqual(error_dict['type'], 'qerror') self.assertAlmostEqual(np.linalg.norm(np.array(4 * [0.25]) - np.array(error_dict['probabilities'])), 0.0) for instr in error_dict['instructions']: self.assertEqual(len(instr), 1) self.assertIn(instr[0]['name'], ['x', 'y', 'z', 'id']) self.assertEqual(instr[0]['qubits'], [0])
def test_dot_both_unitary(self): """Test dot of two unitary errors.""" unitaries0 = [standard_gate_unitary('z'), standard_gate_unitary('s')] probs0 = [0.9, 0.1] unitaries1 = [standard_gate_unitary('x'), standard_gate_unitary('y')] probs1 = [0.6, 0.4] error0 = QuantumError([ np.sqrt(probs0[0]) * unitaries0[0], np.sqrt(probs0[1]) * unitaries0[1] ], standard_gates=False) error1 = QuantumError([ np.sqrt(probs1[0]) * unitaries1[0], np.sqrt(probs1[1]) * unitaries1[1] ], standard_gates=False) error = error1.dot(error0) # Kronecker product unitaries target_unitaries = [ np.dot(unitaries1[0], unitaries0[0]), np.dot(unitaries1[0], unitaries0[1]), np.dot(unitaries1[1], unitaries0[0]), np.dot(unitaries1[1], unitaries0[1]) ] # Kronecker product probabilities target_probs = [ probs1[0] * probs0[0], probs1[0] * probs0[1], probs1[1] * probs0[0], probs1[1] * probs0[1] ] for j in range(4): circ, prob = error.error_term(j) unitary = circ[0]['params'][0] self.assertEqual(circ[0]['name'], 'unitary') self.assertEqual(circ[0]['qubits'], [0]) # Remove prob from target if it is found # later we will check that target_probs is empty so all # the required ones have been removed self.remove_if_found(prob, target_probs) self.remove_if_found(unitary, target_unitaries) # Check we had all the correct target probs and unitaries # by seeing if these lists are empty # Note that this doesn't actually check that the correct # prob was assigned to the correct unitary. self.assertEqual(target_probs, [], msg="Incorrect compose probabilities") self.assertEqual(target_unitaries, [], msg="Incorrect compose unitaries")
def test_pauli_conversion_unitary(self): """Test conversion of Pauli channel kraus to unitary qobj""" a_i = np.sqrt(0.25) * standard_gate_unitary('id') a_x = np.sqrt(0.25) * standard_gate_unitary('x') a_y = np.sqrt(0.25) * standard_gate_unitary('y') a_z = np.sqrt(0.25) * standard_gate_unitary('z') error_dict = QuantumError([a_i, a_x, a_y, a_z], standard_gates=False).to_dict() self.assertEqual(error_dict['type'], 'qerror') self.assertAlmostEqual( np.linalg.norm( np.array(4 * [0.25]) - np.array(error_dict['probabilities'])), 0.0) for instr in error_dict['instructions']: self.assertEqual(len(instr), 1) self.assertIn(instr[0]['name'], ['unitary', 'id']) self.assertEqual(instr[0]['qubits'], [0])
def test_standard_gate_unitary(self, label, matrix): """Test standard gates are correct""" with self.assertWarns( DeprecationWarning, msg=r"standard_gate_unitary has been deprecated as of qiskit-aer 0\.10\.0.*", ): created = standard_gate_unitary(label) self.assertLess(np.linalg.norm(created - matrix), 1e-15)
def test_depolarizing_error_1q_unitary(self): """Test 1-qubit depolarizing error as unitary qobj""" p_depol = 0.3 error = depolarizing_error(p_depol, 1, standard_gates=False) target_unitaries = [standard_gate_unitary('x'), standard_gate_unitary('y'), standard_gate_unitary('z')] for j in range(4): circ, p = error.error_term(j) name = circ[0]['name'] self.assertIn(name, ('unitary', "id")) self.assertEqual(circ[0]['qubits'], [0]) if name == "unitary": self.assertAlmostEqual(p, p_depol / 4, msg="Incorrect Pauli probability") self.remove_if_found(circ[0]['params'], target_unitaries) else: self.assertAlmostEqual(p, 1 - p_depol + p_depol / 4, msg="Incorrect identity probability") self.assertEqual(target_unitaries, [], msg="Incorrect unitaries")
def test_pauli_error_2q_unitary_from_string_1q_only(self): """Test two-qubit pauli error as unitary qobj from string label""" paulis = ['XI', 'YI', 'ZI'] probs = [0.5, 0.3, 0.2] error = pauli_error(zip(paulis, probs), standard_gates=False) target_unitaries = [standard_gate_unitary('x'), standard_gate_unitary('y'), standard_gate_unitary('z')] target_probs = probs.copy() for j in range(len(paulis)): circ, p = error.error_term(j) name = circ[0]['name'] self.assertIn(name, 'unitary') self.assertEqual(circ[0]['qubits'], [1]) self.remove_if_found(p, target_probs) self.remove_if_found(circ[0]['params'], target_unitaries) self.assertEqual(target_probs, [], msg="Incorrect probabilities") self.assertEqual(target_unitaries, [], msg="Incorrect unitaries")
def test_pauli_error_2q_unitary_from_pauli(self): """Test two-qubit pauli error as unitary qobj from Pauli obj""" paulis = [Pauli.from_label(s) for s in ['XY', 'YZ', 'ZX']] probs = [0.5, 0.3, 0.2] error = pauli_error(zip(paulis, probs), standard_gates=False) X = standard_gate_unitary('x') Y = standard_gate_unitary('y') Z = standard_gate_unitary('z') target_unitaries = [np.kron(X, Y), np.kron(Y, Z), np.kron(Z, X)] target_probs = probs.copy() for j in range(len(paulis)): circ, p = error.error_term(j) name = circ[0]['name'] self.assertIn(name, 'unitary') self.assertEqual(circ[0]['qubits'], [0, 1]) self.remove_if_found(p, target_probs) self.remove_if_found(circ[0]['params'], target_unitaries) self.assertEqual(target_probs, [], msg="Incorrect probabilities") self.assertEqual(target_unitaries, [], msg="Incorrect unitaries")
def mixed_unitary_error(self, probs, labels, **kwargs): """Return a Kraus class with the given unitaries (represented by the labels) at the given probabilities, and the same result unitary error list""" with self.assertWarns( DeprecationWarning, msg=r"standard_gate_unitary has been deprecated as of qiskit-aer 0\.10\.0 .*", ): unitaries = [ np.sqrt(prob) * standard_gate_unitary(label) for prob, label in zip(probs, labels) ] with self.assertKrausWarning(): error = QuantumError(unitaries, **kwargs) return Kraus(unitaries), error
def test_standard_gate_unitary(self): """Test standard gates are correct""" def norm(a, b): return round(np.linalg.norm(a - b), 15) self.assertEqual(norm(standard_gate_unitary('id'), np.eye(2)), 0, msg="identity matrix") self.assertEqual(norm(standard_gate_unitary('x'), np.array([[0, 1], [1, 0]])), 0, msg="Pauli-X matrix") self.assertEqual(norm(standard_gate_unitary('y'), np.array([[0, -1j], [1j, 0]])), 0, msg="Pauli-Y matrix") self.assertEqual(norm(standard_gate_unitary('z'), np.diag([1, -1])), 0, msg="Pauli-Z matrix") self.assertEqual(norm(standard_gate_unitary('h'), np.array([[1, 1], [1, -1]]) / np.sqrt(2)), 0, msg="Hadamard gate matrix") self.assertEqual(norm(standard_gate_unitary('s'), np.diag([1, 1j])), 0, msg="Phase gate matrix") self.assertEqual(norm(standard_gate_unitary('sdg'), np.diag([1, -1j])), 0, msg="Adjoint phase gate matrix") self.assertEqual(norm(standard_gate_unitary('t'), np.diag([1, (1 + 1j) / np.sqrt(2)])), 0, msg="T gate matrix") self.assertEqual(norm(standard_gate_unitary('tdg'), np.diag([1, (1 - 1j) / np.sqrt(2)])), 0, msg="Adjoint T gate matrix") self.assertEqual(norm(standard_gate_unitary('cx'), np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0]])), 0, msg="Controlled-NOT gate matrix") self.assertEqual(norm(standard_gate_unitary('cz'), np.diag([1, 1, 1, -1])), 0, msg="Controlled-Z gate matrix") self.assertEqual(norm(standard_gate_unitary('swap'), np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])), 0, msg="SWAP matrix") self.assertEqual(norm(standard_gate_unitary('ccx'), np.array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0]])), 0, msg="Toffoli gate matrix")
def mixed_unitary_error(self, probs, labels): """Return a mixed unitary error list""" return [ np.sqrt(prob) * standard_gate_unitary(label) for prob, label in zip(probs, labels) ]