Gates

Non-unitary but norm-preserving gates, such as measurements. More...

Functions

qreal collapseToOutcome (Qureg qureg, int measureQubit, int outcome)
 Updates qureg to be consistent with measuring measureQubit in the given outcome (0 or 1), and returns the probability of such a measurement outcome. More...
 
int measure (Qureg qureg, int measureQubit)
 Measures a single qubit, collapsing it randomly to 0 or 1. More...
 
int measureWithStats (Qureg qureg, int measureQubit, qreal *outcomeProb)
 Measures a single qubit, collapsing it randomly to 0 or 1, and additionally gives the probability of that outcome. More...
 

Detailed Description

Non-unitary but norm-preserving gates, such as measurements.

Function Documentation

◆ collapseToOutcome()

qreal collapseToOutcome ( Qureg  qureg,
int  measureQubit,
int  outcome 
)

Updates qureg to be consistent with measuring measureQubit in the given outcome (0 or 1), and returns the probability of such a measurement outcome.

This is effectively performing a renormalising projection, or a measurement with a forced outcome. This is an irreversible change to the state, whereby computational states inconsistant with the outcome are given zero amplitude and the qureg is renormalised. The given outcome must not have a near zero probability, else it cannot be collapsed into.

Note that the collapse probably used for renormalisation is calculated for outcome = 0, and assumed 1 minus this probability if outcome = 1. Hence this routine will not correctly project un-normalised quregs onto outcome = 1.

To avoid renormalisation after projection, or force projection into non-physical states with very small probability, use applyProjector().

See also
Parameters
[in,out]quregobject representing the set of all qubits
[in]measureQubitqubit to measure
[in]outcometo force the measure qubit to enter
Returns
probability of the (forced) measurement outcome
Exceptions
invalidQuESTInputError()
  • if measureQubit is outside [0, qureg.numQubitsRepresented)
  • if outcome is not in {0, 1}
  • if the probability of outcome is zero (within machine epsilon)
Author
Ania Brown (state-vector)
Tyson Jones (density matrix)

Definition at line 966 of file QuEST.c.

966  {
967  validateTarget(qureg, measureQubit, __func__);
968  validateOutcome(outcome, __func__);
969 
970  qreal outcomeProb;
971  if (qureg.isDensityMatrix) {
972  outcomeProb = densmatr_calcProbOfOutcome(qureg, measureQubit, outcome);
973  validateMeasurementProb(outcomeProb, __func__);
974  densmatr_collapseToKnownProbOutcome(qureg, measureQubit, outcome, outcomeProb);
975  } else {
976  outcomeProb = statevec_calcProbOfOutcome(qureg, measureQubit, outcome);
977  validateMeasurementProb(outcomeProb, __func__);
978  statevec_collapseToKnownProbOutcome(qureg, measureQubit, outcome, outcomeProb);
979  }
980 
981  qasm_recordMeasurement(qureg, measureQubit);
982  return outcomeProb;
983 }

References densmatr_calcProbOfOutcome(), densmatr_collapseToKnownProbOutcome(), Qureg::isDensityMatrix, qasm_recordMeasurement(), qreal, statevec_calcProbOfOutcome(), statevec_collapseToKnownProbOutcome(), validateMeasurementProb(), validateOutcome(), and validateTarget().

Referenced by TEST_CASE().

◆ measure()

int measure ( Qureg  qureg,
int  measureQubit 
)

Measures a single qubit, collapsing it randomly to 0 or 1.

Outcome probabilities are weighted by the state vector, which is irreversibly changed after collapse to be consistent with the outcome.

The random outcome generator is seeded by seedQuESTDefault() within createQuESTEnv(), unless later overridden by seedQuEST().

See also
Parameters
[in,out]quregobject representing the set of all qubits
[in]measureQubitqubit to measure
Returns
the measurement outcome, 0 or 1
Exceptions
invalidQuESTInputError()
  • if measureQubit is outside [0, qureg.numQubitsRepresented)
Author
Ania Brown (state-vector)
Tyson Jones (density matrix)

Definition at line 998 of file QuEST.c.

998  {
999  validateTarget(qureg, measureQubit, __func__);
1000 
1001  int outcome;
1002  qreal discardedProb;
1003  if (qureg.isDensityMatrix)
1004  outcome = densmatr_measureWithStats(qureg, measureQubit, &discardedProb);
1005  else
1006  outcome = statevec_measureWithStats(qureg, measureQubit, &discardedProb);
1007 
1008  qasm_recordMeasurement(qureg, measureQubit);
1009  return outcome;
1010 }

References densmatr_measureWithStats(), Qureg::isDensityMatrix, qasm_recordMeasurement(), qreal, statevec_measureWithStats(), and validateTarget().

Referenced by TEST_CASE().

◆ measureWithStats()

int measureWithStats ( Qureg  qureg,
int  measureQubit,
qreal outcomeProb 
)

Measures a single qubit, collapsing it randomly to 0 or 1, and additionally gives the probability of that outcome.

Outcome probabilities are weighted by the state vector, which is irreversibly changed after collapse to be consistent with the outcome.

The random outcome generator is seeded by seedQuESTDefault() within createQuESTEnv(), unless later overridden by seedQuEST().

See also
Parameters
[in,out]quregobject representing the set of all qubits
[in]measureQubitqubit to measure
[out]outcomeProba pointer to a qreal which is set to the probability of the occurred outcome
Returns
the measurement outcome, 0 or 1
Exceptions
invalidQuESTInputError()
  • if measureQubit is outside [0, qureg.numQubitsRepresented)
Author
Ania Brown (state-vector)
Tyson Jones (density matrix)

Definition at line 985 of file QuEST.c.

985  {
986  validateTarget(qureg, measureQubit, __func__);
987 
988  int outcome;
989  if (qureg.isDensityMatrix)
990  outcome = densmatr_measureWithStats(qureg, measureQubit, outcomeProb);
991  else
992  outcome = statevec_measureWithStats(qureg, measureQubit, outcomeProb);
993 
994  qasm_recordMeasurement(qureg, measureQubit);
995  return outcome;
996 }

References densmatr_measureWithStats(), Qureg::isDensityMatrix, qasm_recordMeasurement(), statevec_measureWithStats(), and validateTarget().

Referenced by TEST_CASE().

void validateMeasurementProb(qreal prob, const char *caller)
void validateTarget(Qureg qureg, int targetQubit, const char *caller)
void validateOutcome(int outcome, const char *caller)
qreal statevec_calcProbOfOutcome(Qureg qureg, int measureQubit, int outcome)
#define qreal
void densmatr_collapseToKnownProbOutcome(Qureg qureg, int measureQubit, int outcome, qreal outcomeProb)
Renorms (/prob) every | * outcome * >< * outcome * | state, setting all others to zero.
Definition: QuEST_cpu.c:791
void qasm_recordMeasurement(Qureg qureg, int measureQubit)
Definition: QuEST_qasm.c:411
void statevec_collapseToKnownProbOutcome(Qureg qureg, int measureQubit, int outcome, qreal outcomeProb)
int statevec_measureWithStats(Qureg qureg, int measureQubit, qreal *outcomeProb)
Definition: QuEST_common.c:364
int isDensityMatrix
Whether this instance is a density-state representation.
Definition: QuEST.h:325
qreal densmatr_calcProbOfOutcome(Qureg qureg, int measureQubit, int outcome)
int densmatr_measureWithStats(Qureg qureg, int measureQubit, qreal *outcomeProb)
Definition: QuEST_common.c:372