Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
p[0], p[target] = p[target], p[0]
p[1], p[controls[1]] = p[controls[1]], p[1]
elif controls[1] == 2 and controls[0] > 2 and target > 2:
# controls[1] -> target -> outside, controls[0] -> outside
p[1], p[2] = p[2], p[1]
p[1], p[target] = p[target], p[1]
p[0], p[controls[0]] = p[controls[0]], p[0]
else:
p[0], p[controls[0]] = p[controls[0]], p[0]
p1[1], p1[controls[1]] = p1[controls[1]], p1[1]
p2[2], p2[target] = p2[target], p2[2]
p = [p[p1[p2[k]]] for k in range(N)]
return tensor([U] + [identity(2)] * (N - 3)).permute(p)
raise ValueError("target and not control cannot be equal")
p = list(range(N))
if target == 0 and control == 1:
p[control], p[target] = p[target], p[control]
elif target == 0:
p[1], p[target] = p[target], p[1]
p[1], p[control] = p[control], p[1]
else:
p[1], p[target] = p[target], p[1]
p[0], p[control] = p[control], p[0]
return tensor([U] + [identity(2)] * (N - 2)).permute(p)
target : integer
The index of the target qubit.
control_value : integer (1)
The state of the control qubit that activates the gate U.
Returns
-------
result : qobj
Quantum object representing the controlled-U gate.
"""
if [N, control, target] == [2, 0, 1]:
return (tensor(fock_dm(2, control_value), U) +
tensor(fock_dm(2, 1 - control_value), identity(2)))
U2 = controlled_gate(U, control_value=control_value)
return gate_expand_2toN(U2, N=N, control=control, target=target)
# Generate the correct order for qubits permutation,
# eg. if N = 5, targets = [3,0], the order is [1,x,x,0,x].
# If the operator is cnot,
# this order means that the 3rd qubit control the 0th qubit.
new_order = list(range(N))
old_ind = range(len(targets))
for i in old_ind:
new_order[targets[i]] = i
old_ind_set = set(old_ind)
targets_set = set(targets)
mod_value = targets_set.difference(old_ind_set)
mod_ind = old_ind_set.difference(targets_set)
for ind, value in zip(mod_ind, mod_value):
new_order[ind] = value
return tensor([oper] + [identity(2)]*(N-len(targets))).permute(new_order)
elif isinstance(qc, Iterable):
props = qc
gates = None # using list of Qobj, no gates name
else:
raise ValueError(
"qc should be a "
"QubitCircuit or a list of Qobj")
if merge_gates: # merge all gates/Qobj into one Qobj
props = [gate_sequence_product(props)]
gates = None
time_record = [] # a list for all the gates
coeff_record = []
last_time = 0. # used in concatenation of tlist
for prop_ind, U_targ in enumerate(props):
U_0 = identity(U_targ.dims[0])
# If qc is a QubitCircuit and setting_args is not empty,
# we update the kwargs for each gate.
# keyword arguments in setting_arg have priority
if gates is not None and setting_args:
kwargs.update(setting_args[gates[prop_ind]])
full_drift_ham = self.drift.get_ideal_qobjevo(self.dims).cte
full_ctrls_hams = [pulse.get_ideal_qobj(self.dims)
for pulse in self.pulses]
result = cpo.optimize_pulse_unitary(
full_drift_ham, full_ctrls_hams, U_0, U_targ, **kwargs)
if result.fid_err > min_fid_err:
warnings.warn(
"The fidelity error of gate {} is higher "
The index of the target qubit.
Returns
-------
gate : qobj
Quantum object representation of N-qubit gate.
"""
if N < 1:
raise ValueError("integer N must be larger or equal to 1")
if target >= N:
raise ValueError("target must be integer < integer N")
return tensor([identity(2)] * (target) + [U] +
[identity(2)] * (N - target - 1))
raise ValueError("target and not control cannot be equal")
p = list(range(N))
if target == 0 and control == 1:
p[control], p[target] = p[target], p[control]
elif target == 0:
p[1], p[target] = p[target], p[1]
p[1], p[control] = p[control], p[1]
else:
p[1], p[target] = p[target], p[1]
p[0], p[control] = p[control], p[0]
return tensor([U] + [identity(2)] * (N - 2)).permute(p)
return oper_list
# Generate the correct order for qubits permutation,
# eg. if N = 5, targets = [3,0], the order is [1,2,3,0,4].
# If the operator is cnot,
# this order means that the 3rd qubit controls the 0th qubit.
new_order = [0] * N
for i, t in enumerate(targets):
new_order[t] = i
# allocate the rest qutbits (not targets) to the empty
# position in new_order
rest_pos = [q for q in list(range(N)) if q not in targets]
rest_qubits = list(range(len(targets), N))
for i, ind in enumerate(rest_pos):
new_order[ind] = rest_qubits[i]
id_list = [identity(dims[i]) for i in rest_pos]
return tensor([oper] + id_list).permute(new_order)