I am trying to create a unitary operator $U = \sum^{T - 1}_{k=0}$ $|k\rangle$ $\langle k |$ $ \otimes$ $e^{i A k}$ in Q#, where A is a Hermitian matrix. For the beginning, I just want A to be a combination of 2 Pauli matrices, say $A = X + 2Z$. This is the code that I have, where input
is of type Qubit[3]
and register
of Qubit[2]
:
...
let SIZE_OF_MATRIX = 2;
let unitaryGenerator = (2 ^ SIZE_OF_MATRIX, ConstructU);
let registerLE = LittleEndian(register);
MultiplexOperationsFromGenerator(unitaryGenerator, registerLE, input);
...
function ConstructU (j : Int) : (Qubit[] => Unit is Adj + Ctl) {
let generatorSystem = GeneratorSystem(2, MapToGeneratorIndex);
let evolutionGenerator = EvolutionGenerator(PauliEvolutionSet(), generatorSystem);
let unitaryOperator = TrotterStep(evolutionGenerator, 1, - IntAsDouble(j));
return unitaryOperator;
}
// The purpose of this function is to map each part of the Hamiltonian generator
// to a generator index.
// Initially, we want to test the matrix A = X + 2Z.
function MapToGeneratorIndex (index : Int) : GeneratorIndex {
// We only have 2 terms, hence index can only be 0 or 1
if (index == 0) {
// Here we just want X
return GeneratorIndex(([1], [1.0]), [0]);
}
elif (index == 1) {
// Here we want 2Z
return GeneratorIndex(([3], [2.0]), [0]);
}
// TODO: throw an error
return GeneratorIndex( ([1000], [1000.0]), [0]);
}
Does anyone know what am I doing incorrectly? I am not getting the result that I am expecting. I know the code is messy but I am just trying to make it work for a basic 2x2 matrix first.
Thanks for the help!
MultiplexOperationsFromGenerator
to expect four unitary operations.Fst(unitaryGenerator)
should specify the number of operations returned bySnd(unitaryGenerator)
. In this case, if I understand your definition ofMapToGeneratorIndex
, that meansFst(unitaryGenerator)
should be2
instead of2 ^ SIZE_OF_MATRIX
. $\endgroup$2 ^ SIZE_OF_MATRIX
, so I am indeed expecting 4 unitary operations. How many matrices are used in the Hamiltonian should be specified in theGeneratorSystem
, which I did in the first line of theConstructU
function, and they are 2 indeed. $\endgroup$input
register using theDumpRegister
function right after I applyU
, the resulting state is 0 both in the|0>
and|1>
components for some reason. $\endgroup$