microsoft / QuantumLibraries

Q# libraries for the Quantum Development Kit

Home Page:https://docs.microsoft.com/quantum

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inserting `MultiM` Operator into circuit will change the states of the qubits

weucode opened this issue · comments

Description

When running the following Q# program, the result of each shots is different for two qubit arrays. But the measure operation used here should not have changed the state of qubits. Here are just one situation, it may be quite different in each time.

mid:
[Zero,Zero,Zero,Zero,Zero,Zero]
[One,Zero,Zero,Zero]
final:
[Zero,Zero,Zero,Zero,Zero,Zero]
[Zero,Zero,Zero,One]
mid:
[Zero,Zero,Zero,Zero,Zero,Zero]
[One,Zero,Zero,Zero]
final:
[Zero,Zero,Zero,Zero,Zero,Zero]
[One,Zero,Zero,Zero]

But if we comment the lines which tag with "// Codes that have an impact", the final state will be zero all the time.

final:
[Zero,Zero,Zero,Zero,Zero,Zero]
[Zero,Zero,Zero,Zero]

It is supposed that QsCompiler will execute some passes when get code block like op and adjoint op, but if there are some other operations between op and adjoint op, it will not work even when these operations do nothing.
Another probability is that MultiM operation does change the state here.

Testcase

namespace NISLNameSpace {
	open Microsoft.Quantum.Intrinsic;
	open Microsoft.Quantum.Measurement;
	open Microsoft.Quantum.ErrorCorrection;


	operation NISLMeasure (q : Qubit[]) : Unit { 
        mutable result = MultiM (q);
        Message($"{result}");		
	}

	operation NISLOperation () : Unit { 
		use NISLVariable1 = Qubit[6];
		use NISLVariable2 = Qubit[4];
		FiveQubitCodeEncoderImpl(NISLVariable1,NISLVariable2);
		Message("mid:");                    // Codes that have an impact
                NISLMeasure(NISLVariable1); // Codes that have an impact
		NISLMeasure(NISLVariable2); // Codes that have an impact
		Adjoint FiveQubitCodeEncoderImpl(NISLVariable1,NISLVariable2);		
		Message("final:");
		NISLMeasure(NISLVariable1);
		NISLMeasure(NISLVariable2);
		ResetAll(NISLVariable1);
		ResetAll(NISLVariable2);
	}

	@EntryPoint()
	operation main() : Unit {
		for i in 0..5{
			mutable result = NISLOperation();
		}
	}
}

Environment

operating system : Windows 10

dotnet version : 6.0.301

QDK : 0.25.218240


文档详细信息

请勿编辑此部分。 docs.microsoft.com ➟ GitHub 问题链接必须具有此部分。

Thanks for filing this issue @weucode. Can you try using MeasureEachZ instead?

I'm sorry it took so long to get back to you, but it still does not work by using MeasureEachZ. Here is one shot.

mid:
[Zero,Zero,Zero,Zero,Zero,Zero]
[One,Zero,Zero,Zero]
final:
[Zero,Zero,Zero,Zero,Zero,Zero]
[Zero,Zero,Zero,One]

By the way, can you tell me if one of the two conjectures mentioned above are correct?

Thanks for adding more information. Can you please share the implementation of FiveQubitCodeEncoderImpl such I can try to reproduce the behavior on my end.

Thanks for your reply! This is not a user-defined API, I find FiveQubitCodeEncoderImpl on this page.