Nerve to quadratus femoris

The nerve to quadratus femoris is a nerve of the sacral plexus that provides motor innervation to the quadratus femoris muscle and gemellus inferior muscle, and an articular branch to the hip joint. The nerve leaves the pelvis through the greater sciatic foramen.

Origin
The nerve to quadratus femoris is a branch of the sacral plexus. It arises from the anterior divisions of the spinal nerves L4-S1.

Course
It exits the pelvis through the greater sciatic foramen inferior to the piriformis muscle.

It passes inferior-ward upon the ischium deep to the sciatic nerve,   the superior and inferior gemellus muscles, and the tendon of the obturator internus. It traverses the posterior aspect of the hip joint,  here issuing an articular (sensory) branch to the joint.   It proceeds inferior-ward deep to the superior and inferior gemelli muscles and the obturator internus muscle. 

It enters the anterior surfaces of quadratus femoris muscle  and gemellus inferior muscle.

Variation
Rarely, the nerve to quadratus femoris may also innervate the gemellus superior muscle, or the upper part of adductor magnus muscle.

Function
The nerve to quadratus femoris provides motor innervation to quadratus femoris muscle and inferior gemellus muscle. It also provides sensory innervation to the hip joint.