Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
single_qubit_gates: List[List[ops.SingleQubitGate]] = None,
benchmark_ops: Sequence[ops.Moment] = None,
) -> List[circuits.Circuit]:
if benchmark_ops is not None:
num_d = len(benchmark_ops)
else:
num_d = 0
max_cycles = max(cycles)
if single_qubit_gates is None:
single_rots = _random_half_rotations(qubits, max_cycles)
else:
single_rots = _random_any_gates(qubits, single_qubit_gates, max_cycles)
all_circuits = [] # type: List[circuits.Circuit]
for num_cycles in cycles:
circuit_exp = circuits.Circuit()
for i in range(num_cycles):
circuit_exp.append(single_rots[i])
if benchmark_ops is not None:
for op_set in benchmark_ops[i % num_d]:
circuit_exp.append(op_set)
all_circuits.append(circuit_exp)
return all_circuits
def _cleanup_operations(operations: List[ops.Operation]):
circuit = circuits.Circuit(operations)
optimizers.merge_single_qubit_gates.\
merge_single_qubit_gates_into_phased_x_z(circuit)
optimizers.eject_phased_paulis.EjectPhasedPaulis().optimize_circuit(circuit)
optimizers.eject_z.EjectZ().optimize_circuit(circuit)
circuit = circuits.Circuit(circuit.all_operations(),
strategy=circuits.InsertStrategy.EARLIEST)
return list(circuit.all_operations())
def to_circuit(self) -> Circuit:
"""Convert the schedule to a circuit.
This discards most timing information from the schedule, but does place
operations that are scheduled at the same time in the same Moment.
"""
circuit = Circuit(device=self.device)
time: Optional[Timestamp] = None
for so in self.scheduled_operations:
if so.time != time:
circuit.append(so.operation,
strategy=InsertStrategy.NEW_THEN_INLINE)
time = so.time
else:
circuit.append(so.operation,
strategy=InsertStrategy.INLINE)
return circuit
def swap_network(self) -> SwapNetwork:
return SwapNetwork(circuits.Circuit(self.physical_ops),
self.initial_mapping)
) -> 'cirq.Circuit':
moments = []
for i, moment_proto in enumerate(circuit_proto.moments):
moment_ops = []
for op in moment_proto.operations:
try:
moment_ops.append(
self.deserialize_op(
op, arg_function_language=arg_function_language))
except ValueError as ex:
raise ValueError(f'Failed to deserialize circuit. '
f'There was a problem in moment {i} '
f'handling an operation with the '
f'following proto:\n{op}') from ex
moments.append(ops.Moment(moment_ops))
return circuits.Circuit(moments)
def circuit(self) -> 'cirq.Circuit':
result = circuits.Circuit()
for col in self._sub_cell_cols_sealed():
body = circuits.Circuit(
cell.operations() for cell in col if cell is not None)
if body:
basis_change = circuits.Circuit(
cell.basis_change() for cell in col if cell is not None)
result += basis_change
result += body
result += basis_change**-1
return result
is determined by the qubit order argument (which defaults to just
sorting the qubits that are present into an ascending order).
"""
if not isinstance(initial_state, int):
initial_state = np.asarray(initial_state, dtype=dtype)
if isinstance(program, (schedules.Schedule, circuits.Circuit)):
# No change needed.
pass
elif isinstance(program, ops.Gate):
program = circuits.Circuit(
program.on(*devices.LineQid.for_gate(program)))
else:
# It should be an OP_TREE.
program = circuits.Circuit(program)
if not protocols.has_unitary(
protocols.resolve_parameters(program, param_resolver)):
raise ValueError(
"Program doesn't have a single well defined final wavefunction "
"because it is not unitary. "
"Maybe you wanted `cirq.sample_wavefunction`?\n"
"\n"
"Program: {!r}".format(program))
result = sparse_simulator.Simulator(dtype=dtype, seed=seed).simulate(
program=cast(Union[circuits.Circuit, schedules.Schedule], program),
initial_state=initial_state,
qubit_order=qubit_order,
param_resolver=param_resolver)