Main Content

formula

Formula representation of quantum state

Since R2023a

Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.

Description

str = formula(s) returns the formula of the specified quantum state as a string.

example

[str,states,amplitudes] = formula(s) additionally returns the basis states a string vector and the amplitudes as a numeric vector, both of which are used to form the output formula of the quantum state.

example

___ = formula(s,Name=Value) specifies options using one or more name-value arguments. You can use this syntax with any of the output argument combinations in the previous syntaxes.

For example, you can choose the basis in which to represent each qubit by using formula(s,Basis=basis). You can also specify a probability threshold to include states in the output by using formula(s,Threshold=thr).

example

Examples

collapse all

Create a quantum circuit that consists of a controlled X gate acting on two qubits.

c = quantumCircuit(cxGate(1,2));

Simulate the circuit using four initial states: "00", "01", "10", and "11". Display the final states after running the simulation as formulas.

for inState=["00","01","10","11"]
  s = simulate(c,inState);
  disp("|" + inState + "> -> " + formula(s))
end
|00> -> 1 * |00>
|01> -> 1 * |01>
|10> -> 1 * |11>
|11> -> 1 * |10>

Create a quantum circuit that consists of a controlled X gate acting on two qubits.

c = quantumCircuit(cxGate(1,2));

Simulate the circuit using four initial states: "00", "01", "10", and "11". Use formula to extract the amplitudes and the final states after simulating the circuit.

Use assert to check if these conditions hold:

  • The amplitude of the final state is equal to 1.

  • If the control qubit is in the |0 state (the state of qubit 1 is "0"), then this gate does nothing, where the initial state is equal to the final state.

  • If the control qubit is in the |1 state (the state of qubit 1 is "1"), then this gate applies the NOT gate to the target qubit, where the state of qubit 2 is flipped.

for inState=["00","01","10","11"]
  s = simulate(c,inState);
  [str,outState,amps] = formula(s);
  assert(isequal(amps,1));

  % Map state to logical vector, where '1' is true and '0' is false
  sIn = char(inState) == '1';
  sOut = char(outState) == '1';

  % Compare sOut to expectation
  if sIn(1) == false
    assert(isequal(sIn,sOut));
  else
    assert(isequal(sOut,[sIn(1) ~sIn(2)]));
  end
end

Create a quantum state for a single qubit.

s = quantum.gate.QuantumState([0.7143 0.6998])
s = 

  QuantumState with properties:

    BasisStates: [2×1 string]
     Amplitudes: [2×1 double]
      NumQubits: 1

Display the quantum state as a formula in the default Z basis (where X basis is chosen for any qubit if this simplifies the formula).

str = formula(s)
str = 

    "0.71432 * |0> +
     0.69982 * |1>"

Display the quantum state as a formula in the X basis by specifying the Basis name-value argument as "X".

str = formula(s,Basis="X")
str = 

    "0.99995  * |+> +
     0.010253 * |->"

Display the formula including only states with a probability greater than 0.01. Specify the probability threshold as 0.01 by using the Threshold name-value argument. Because the | state has a probability amplitude of about 0.01, or its measurement probability is about 0.0001 (magnitude squared of the amplitude), this state is not displayed in the output formula.

str = formula(s,Basis="X",Threshold=0.01)
str = 

    "0.99995 * |+>"

Input Arguments

collapse all

Quantum state, specified as a QuantumState object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: formula(quantum.gate.QuantumState("101"),Basis="X")

Basis in which to represent each qubit, specified as one of these values:

  • "auto" (default) — Return the formula in the Z basis (the "0" and "1" states), but use the X basis for any qubit that can always be represented in the "+" or "-" state.

  • "Z" — Return the formula in the Z basis (the "0" and "1" states).

  • "X" — Return the formula in the X basis (the "+" and "-" states).

  • String containing "Z" and "X" — Return the formula using the Z or X basis specified for each qubit in the input quantum state. The length of the string must equal the number of qubits in the circuit.

Probability threshold to include states, specified as a real number or "none". The default threshold is a real number equal to s.NumQubits*eps("like",s.Amplitudes). The probability of measuring a state is the magnitude squared of the amplitude of that state, as shown in the formula.

Setting the threshold to "none" shows all possible states in the formula, including states with 0 probability.

Output Arguments

collapse all

Formula of the quantum state, returned as a string scalar.

Each term of the formula is separated by a newline character. To represent the whole formula on a single line, you can use str = replace(str,newline," ").

Basis states of the quantum state, returned as a string vector. The combination of amplitudes and states forms the output formula of the quantum state.

Amplitudes of the basis states, returned as a vector of numbers. These amplitudes are normalized, where the sum of the probabilities of the basis states (magnitude squared of the amplitudes) is 1. The combination of amplitudes and states forms the output formula of the quantum state.

Version History

Introduced in R2023a