def test_two_qubit_decomposition_1_cnot(self, U, wires): """Test that a two-qubit matrix using one CNOT is correctly decomposed.""" U = _convert_to_su4(np.array(U)) assert _compute_num_cnots(U) == 1 obtained_decomposition = two_qubit_decomposition(U, wires=wires) assert len(obtained_decomposition) == 5 with qml.tape.QuantumTape() as tape: for op in obtained_decomposition: qml.apply(op) obtained_matrix = get_unitary_matrix(tape, wire_order=wires)() assert check_matrix_equivalence(U, obtained_matrix, atol=1e-7)
def test_two_qubit_decomposition_tensor_products(self, U_pair, wires): """Test that a two-qubit tensor product matrix is correctly decomposed.""" U = _convert_to_su4( qml.math.kron(np.array(U_pair[0]), np.array(U_pair[1]))) assert _compute_num_cnots(U) == 0 obtained_decomposition = two_qubit_decomposition(U, wires=wires) assert len(obtained_decomposition) == 2 with qml.tape.QuantumTape() as tape: for op in obtained_decomposition: qml.apply(op) obtained_matrix = get_unitary_matrix(tape, wire_order=wires)() assert check_matrix_equivalence(U, obtained_matrix, atol=1e-7)
def test_two_qubit_decomposition_3_cnots(self, U, wires): """Test that a two-qubit matrix using 3 CNOTs is correctly decomposed.""" U = _convert_to_su4(np.array(U)) assert _compute_num_cnots(U) == 3 obtained_decomposition = two_qubit_decomposition(U, wires=wires) assert len(obtained_decomposition) == 10 with qml.tape.QuantumTape() as tape: for op in obtained_decomposition: qml.apply(op) obtained_matrix = get_unitary_matrix(tape, wire_order=wires)() # We check with a slightly great tolerance threshold here simply because the # test matrices were copied in here with reduced precision. assert check_matrix_equivalence(U, obtained_matrix, atol=1e-7)