Perl script to compute state fidelity