// Parses MCF wavefunction.
        internal static SparseMultiCFWavefunction <SpinOrbital> ToSparseMultiCFWavefunction(List <List <string> > superposition)
        {
            var outputState = new SparseMultiCFWavefunction <SpinOrbital>();

            // Todo: modify broombridge to have explicit reference state.
            foreach (var element in superposition)
            {
                // First item is the amplitude.
                double amplitude = double.Parse(element.First().ToString(), System.Globalization.CultureInfo.InvariantCulture);

                // Second to Second-last are fermion operators.
                IEnumerable <LadderOperator <SpinOrbital> > operators = element
                                                                        .Take(element.Count() - 1)
                                                                        .Skip(1)
                                                                        .Select(o => V0_1.ParsePolishNotation(o.ToString()));

                var ladderSequence = new LadderSequence <SpinOrbital>(operators);

                // Sort operators to index order.
                IEnumerable <IndexOrderedSequence <SpinOrbital> > sortedOperators = ladderSequence.ToIndexOrder();

                // Only select the shortest term with no repeated indices.
                IndexOrderedSequence <SpinOrbital> sortedOperator = sortedOperators.ToList().OrderBy(o => o.UniqueIndices()).First();

                outputState.Set(sortedOperator, new Complex(amplitude, 0.0));
            }
            return(outputState);
        }
        /// <summary>
        /// Builds Hamiltonian from Broombridge if data is available.
        /// </summary>
        internal static OrbitalIntegralHamiltonian ToOrbitalIntegralHamiltonian(ProblemDescription broombridge)
        {
            // Add the identity terms
            var identityterm = broombridge.CoulombRepulsion.Value + broombridge.EnergyOffset.Value;
            var hamiltonian  = V0_1.ToOrbitalIntegralHamiltonian(broombridge.Hamiltonian);

            hamiltonian.Add(new OrbitalIntegral(), identityterm);
            return(hamiltonian);
        }
        // Parses UCC wavefunction.
        internal static UnitaryCCWavefunction <SpinOrbital> ToUnitaryCCWavefunction(ClusterOperator clusterOperator)
        {
            var outputState = new UnitaryCCWavefunction <SpinOrbital>();

            var oneBodyTerms = clusterOperator.OneBodyAmplitudes ??
                               new List <List <string> >();

            var twoBodyTerms = clusterOperator.TwoBodyAmplitudes ??
                               new List <List <string> >();

            foreach (var element in oneBodyTerms.Concat(twoBodyTerms))
            {
                // First item is the amplitude.
                double amplitude = double.Parse(element.First().ToString(), System.Globalization.CultureInfo.InvariantCulture);

                // Second to last are fermion operators.
                IEnumerable <LadderOperator <SpinOrbital> > operators = element
                                                                        .Skip(1)
                                                                        .Select(o => V0_1.ParsePolishNotation(o.ToString()));

                var ladderSequence = new LadderSequence <SpinOrbital>(operators);

                // Terms are assumed to be in normal order.
                var sortedOperator = new IndexOrderedSequence <SpinOrbital>(operators);

                outputState.Set(sortedOperator, new Complex(amplitude, 0.0));
            }

            var reference = new List <List <string> >()
            {
                clusterOperator.Reference
            };

            outputState.Reference = new SingleCFWavefunction <SpinOrbital>(
                ToSparseMultiCFWavefunction(reference).Excitations.Single().Key.ToIndices()
                );

            return(outputState);
        }