In principle one can use Pontryagin's minimum principle to treat an optimal control problem and derive the gradient for the cost functional. However, for the driven spin-boson model studied here, the response of the system to the variation of the control is determined by the master equation and the equation of motion for the propagator of the coherent system dynamics. As a result, the application of Pontryagin's minimum principle is less straightforward. An alternative to this approach is the technique of automatic differentiation which in principle amounts to doing calculus on the fully discretized form of the optimal control problem. Automatic differentiation tools can be viewed as black boxes taking as input a program computing the cost functional and giving as output another program computing its gradient. First we derive for the polaron transformed Hamiltonian a Born-Markov master equation using the Bloch-Redfield formalism. By combining the latter with automatic differentiation we are able to implement Z-gate and coherent destruction of tunnelling with high fidelity. Optimization of a dissipative quantum gate poses a more complex numerical problem, since it should occur independent of the input state. To overcome this difficulty, we apply optimal control to the concept of resolvent of the master equation which is the generalisation of quantum unitary evolution operator in the case of dissipative dynamics.