mimiqcircuits.equivalence

Experiment to check equivalence between two circuits.

Classes

CircuitTesterExperiment(*args, **kwargs)

Deprecated alias of EquivalenceExperiment.

EquivalenceExperiment(c1, c2[, method])

An experiment that checks whether two unitary circuits are equivalent.

class EquivalenceExperiment(c1, c2, method='samples')[source]

Bases: object

An experiment that checks whether two unitary circuits are equivalent.

The test uses the Choi-Jamiolkowski isomorphism. Each of the \(n\) input qubits is entangled with an ancilla in a Bell pair, then c1 and the inverse of c2 are applied to the input qubits, and the Bell pairs are undone. The \(2n\) qubits end in the all-zero state with probability

\[P = \left| \frac{\operatorname{Tr}(U_2^\dagger U_1)}{2^n} \right|^2,\]

which is 1 exactly when the two circuits implement the same unitary up to a global phase. build_circuit() returns this test circuit and interpret_results() reads \(P\) from its results.

Both circuits must be invertible, so they cannot contain measurements, resets or noise.

Parameters:
  • c1 (Circuit) – The first circuit.

  • c2 (Circuit) – The second circuit.

  • method (str) – How \(P\) is read. "samples" (the default) measures all qubits and counts the all-zero outcomes; "amplitudes" computes the amplitude of the all-zero state exactly.

Raises:

ValueError – If method is not "samples" or "amplitudes".

Examples

>>> from mimiqcircuits import *
>>> # HZH and X are the same gate
>>> c1 = Circuit().push(GateH(), 0).push(GateZ(), 0).push(GateH(), 0)
>>> c2 = Circuit().push(GateX(), 0)
>>> exp = EquivalenceExperiment(c1, c2, method="amplitudes")
>>> exp
Choi-isomorphism circuit equivalence test:
├── a == b
│   ├── a: 1-qubit circuit with 3 instructions
│   └── b: 1-qubit circuit with 1 instruction
└── compare amplitudes
>>> # run the test circuit remotely and read the probability
>>> conn = MimiqConnection()  
>>> conn.connect()  
>>> job = conn.submit(exp)  
>>> exp.interpret_results(conn.get_result(job))  
1.0
build_circuit()[source]

Return the test circuit of the experiment.

It uses twice as many qubits as the larger of the two circuits: the input qubits come first, the ancillas after them. With method="samples" it ends by measuring every qubit into the bit of the same index; with method="amplitudes" it ends with an Amplitude of the all-zero state stored in z-variable 0. When both circuits are empty the test circuit is empty too.

Returns:

The test circuit.

Return type:

Circuit

Raises:

Examples

>>> from mimiqcircuits import *
>>> c = Circuit().push(GateX(), 0)
>>> EquivalenceExperiment(c, c, method="amplitudes").build_circuit()
2-qubit, 1-zvar circuit with 7 instructions:
├── H @ q[0]
├── CX @ q[0], q[1]
├── X @ q[0]
├── X @ q[0]
├── CX @ q[0], q[1]
├── H @ q[0]
└── Amplitude(bs"00") @ z[0]
interpret_results(results)[source]

Return the probability of the all-zero state from the results of the test circuit.

With method="samples" this is the fraction of samples that measured all zeros. With method="amplitudes" it is the squared modulus of the computed amplitude, averaged over the results that have one. A value of 1 means the circuits are equivalent up to a global phase. When both circuits are empty the result is 1.0; when the results hold no samples or amplitudes it is 0.0.

Parameters:

results (QCSResults) – The results of running build_circuit().

Returns:

The probability, between 0 and 1.

Return type:

float

saveproto(file)[source]

Save the experiment to a protobuf (binary) file.

Parameters:

file – A file path or a binary file-like object to write to.

Returns:

The number of bytes written.

Return type:

int

static loadproto(file)[source]

Load an experiment saved with saveproto().

Parameters:

file – A file path or a binary file-like object to read from.

Returns:

The loaded experiment.

Return type:

EquivalenceExperiment

class CircuitTesterExperiment(*args, **kwargs)[source]

Bases: EquivalenceExperiment

Deprecated alias of EquivalenceExperiment.

Creating one emits a DeprecationWarning.