Quadrupole magnet

Quadrupole magnets, abbreviated as Q-magnets, consist of groups of four magnets laid out so that in the planar multipole expansion of the field, the dipole terms cancel and where the lowest significant terms in the field equations are quadrupole. Quadrupole magnets are useful as they create a magnetic field whose magnitude grows rapidly with the radial distance from its longitudinal axis. This is used in particle beam focusing.
The simplest magnetic quadrupole is two identical bar magnets parallel to each other such that the north pole of one is next to the south of the other and vice versa. Such a configuration will have no dipole moment, and its field will decrease at large distances faster than that of a dipole. A stronger version with very little external field involves using a "k"=3 Halbach cylinder.
In some designs of quadrupoles using electromagnets, there are four steel pole tips: two opposing magnetic north poles and two opposing magnetic south poles. The steel is magnetized by a large electric current in the coils of tubing wrapped around the poles. Another design is a Helmholtz coil layout but with the current in one of the coils reversed.
Quadrupoles in particle accelerators.
At the particle speeds reached in high energy particle accelerators, the magnetic force term is larger than the electric term in the Lorentz force:
and thus magnetic deflection is more effective than electrostatic deflection. Therefore a 'lattice' of electromagnets is used to bend, steer and focus a charged particle beam. 
The quadrupoles in the lattice are of two types: 'F quadrupoles' (which are horizontally focusing but vertically defocusing) and 'D quadrupoles' (which are vertically focusing but horizontally defocusing). This situation is due to the laws of electromagnetism (the Maxwell equations) which show that it is impossible for a quadrupole to focus in both planes at the same time. The image on the right shows an example of a quadrupole focusing in the vertical direction for a positively charged particle going into the image plane (forces above and below the center point towards the center) while defocusing in the horizontal direction (forces left and right of the center point away from the center). 
If an F quadrupole and a D quadrupole are placed immediately next to each other, their fields completely cancel out (in accordance with Earnshaw's theorem). But if there is a space between them (and the length of this has been correctly chosen), the overall effect is focusing in both horizontal and vertical planes. A lattice can then be built up enabling the transport of the beam over long distances—for example round an entire ring. A common lattice is a FODO lattice consisting of a basis of a focusing quadrupole, 'nothing' (often a bending magnet), a defocusing quadrupole and another length of 'nothing'.
Mathematical description of the ideal field.
The components of the ideal magnetic field in the plane transverse to the beam is given by (see also multipole magnet):
if the magnetic poles are arranged with an angle of 45 degrees to the horizontal and vertical planes. formula_3 is the field gradient
of the vertical component in the horizontal direction (or equivalently, the field gradient of the horizontal component in the vertical direction). Its SI unit is formula_4. The sign of formula_3 determines whether (for a fixed particle charge and direction) the quadrupole focuses or defocuses particles in the horizontal plane.