As far as I understand, "targets" is a basis (of the qubits system) to which gate is applied. For instance, Hadamard gate takes basis of a single qubit system, and CNOT gate takes a basis vector of a 2-qubit system. However, it is possible to enter an arbitrary number of 0 and 1', so I'm afraid I misunderstand what "targets" really are.
1 Answers
In QuTiP parameter targets of Gate and Measurement objects is an index to qubit lines (register) in QubitCircuit and QubitCircuit object is representation of a quantum program/algorithm, maintaining a sequence of gates and measurements.
Those gates and measurements are applied on qubit lines and qubit lines have form of indexed array so it is simply index of qubit line in qubit register (circuit).
There is similar parameter controls of Gate object to define qubit lines used as control lines in Gate object.
The data defined with basis is the State object, which is provided later when defining which circuit lines are used as inputs/outputs.
Both targets and controls can be defined as single int or list of ints.
The state is defined as string.
How that looks in the code:
Single qubit gate:
num_qubits = 2
circuit = QubitCircuit(num_qubits)
circuit.add_gate("X", targets=0) # single int
Multiple qubit gates:
N = 3
qc = QubitCircuit(N)
qc.add_gate("FREDKIN", targets=[0, 1], controls=[2]) # single int and int list
qc.add_gate("TOFFOLI", controls=[0, 1], targets=[2]) # single int and int list
qc.add_gate("CTRLRX", targets=[1, 2], arg_value=np.pi/2) # int list
qc.add_gate("CTRLMAT3", targets=[1, 0], arg_value=1) # int list
Measurements:
qc.add_measurement("M0", targets=[0], classical_store=0) # single int
qc.add_measurement("M1", targets=[1], classical_store=1) # single int
Once the QubitCircuit is assembled we can define which circuit lines are inputs/outputs with method:
def add_state(self, state, targets=None, state_type="input"):
"""
Add an input or ouput state to the circuit. By default all the input
and output states will be initialized to `None`. A particular state can
be added by specifying the state and the qubit where it has to be added
along with the type as input or output.
Parameters
----------
state: str
The state that has to be added. It can be any string such as `0`,
'+', "A", "Y"
targets: list
A list of qubit positions where the given state has to be added.
state_type: str
One of either "input" or "output". This specifies whether the state
to be added is an input or output.
default: "input"
"""
if state_type == "input":
for i in targets:
self.input_states[i] = state
if state_type == "output":
for i in targets:
self.output_states[i] = state
Here again we have targets (but now being method parameter) and again it can be list of int or single int. :
qc = QubitCircuit(3)
qc.add_state("up", targets=[0])
qc.add_state("down", targets=[1])
In the above we have initialized qubit lines 1 and 2 with states "up" and "down" and those states may be defined with basis:
up = basis(2, 0)
down = basis(2, 1)
So up and down are our ket vectors which are initializing circuit and may be printed on Bloch sphere as well:
b = qutip.Bloch()
up = qutip.basis(2, 0)
b.add_states(up)
b.render()
Note slight difference of using state name with or without apostrophes.

- 379
- 1
- 9
\
code`` to format inline code – glS May 07 '22 at 20:12