def print_gate(t): print('t =', t) matrix = U00(t) gates = qd.matrix_to_gates(matrix) circuit = qd.matrix_to_cirq_circuit(matrix) print(circuit) print()
def matrix_to_sycamore_operations( target_qubits: List[cirq.GridQubit], matrix: np.ndarray, ) -> Tuple[cirq.OP_TREE, List[cirq.GridQubit]]: """Handle cases for 1 (and 2) qubit(s) separately. A method to convert a unitary matrix to a list of Sycamore operations. This method will return a list of `cirq.Operation`s using the qubits and (optionally) ancilla qubits to implement the unitary matrix `matrix` on the target qubits `qubits`. The operations are also supported by `cirq.google.gate_sets.SYC_GATESET`. Args: target_qubits: list of qubits the returned operations will act on. The qubit order defined by the list is assumed to be used by the operations to implement `matrix`. matrix: a matrix that is guaranteed to be unitary and of size (2**len(qs), 2**len(qs)). Returns: A tuple of operations and ancilla qubits allocated. Operations: In case the matrix is supported, a list of operations `ops` is returned. `ops` acts on `qs` qubits and for which `cirq.unitary(ops)` is equal to `matrix` up to certain tolerance. In case the matrix is not supported, it might return NotImplemented to reduce the noise in the judge output. Ancilla qubits: In case ancilla qubits are allocated a list of ancilla qubits. Otherwise an empty list. """ unitaries = decompose_to_two_level_unitaries(target_qubits.count(), matrix) # ops = decompose_unitaries_to_ops(target_qubits, unitaries) # Validate ops with cirq.google.gate_sets.SYC_GATESET and return accordingly gates = qd.matrix_to_cirq_circuit(matrix) # print(gates) return NotImplemented, []
def random_matrix( target_qubits: List[cirq.GridQubit], matrix: np.ndarray, swap=True, ) -> Tuple[cirq.OP_TREE, List[cirq.GridQubit]]: circuit = quantum_decomp.matrix_to_cirq_circuit(matrix) old_qubits = [(str(x), x) for x in circuit.all_qubits()] old_qubits = sorted(old_qubits) old_qubits = [x[1] for x in old_qubits] mapping = dict(zip(old_qubits, target_qubits)) print(mapping) decomp_ops = [] ops = circuit.all_operations() for op in ops: if type(op) == cirq.ops.controlled_operation.ControlledOperation: gate = op controls = gate.controls target = gate.sub_operation.qubits[0] matrix = cirq.unitary(gate.sub_operation.gate) decomp_ops.extend([ x.transform_qubits(mapping) for x in cirq.optimizers.decompose_multi_controlled_rotation( matrix, list(controls), target) ]) else: decomp_ops.append(op.transform_qubits(mapping)) swapped_ops = [] for op in decomp_ops: if len(op.qubits) == 2: q0 = op.qubits[0] q1 = op.qubits[1] if swap: oplist, target = do_swap(q0, q1) adjacentop = op.transform_qubits({q0: target, q1: q1}) revlist = swap_to(target, q0) swapped_ops.extend(oplist) swapped_ops.append(adjacentop) swapped_ops.extend(revlist) else: swapped_ops.append(op) else: swapped_ops.append(op) SycamoreGates = cirq.google.optimized_for_sycamore( cirq.Circuit(swapped_ops), optimizer_type='sycamore', ) return SycamoreGates, []
[0, 0, 0, np.exp(-1j * t)]]) U11 = np.array([[np.exp(1j * t), 0, 0, 0], [0, (np.exp(1j * t) + 1) / 2, np.expm1(1j * t) / 2, 0], [0, np.expm1(1j * t) / 2, (np.exp(1j * t) + 1) / 2, 0], [0, 0, 0, 1]]) Up = np.array([[np.exp(1j * t), 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, np.exp(-1j * t)]]) Um = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) return np.kron(o0000, U00) + np.kron(o1111, U11) + np.kron( opp, Up) + np.kron(omm, Um) # for i,t in enumerate(np.arange(0, 2*π+0.1, π/8)): # print('t =', str(i) + 'π/8') # print(qd.matrix_to_cirq_circuit(U0_matrix(t), optimize=True)) # for matrix in qd.two_level_decompose(U0_matrix(π/8)): # print(matrix) def keep_fuction(oper: cirq.Operation) -> bool: print(oper, oper.gate.num_qubits()) return oper.gate.num_qubits() <= 2 print(qd.matrix_to_cirq_circuit(U0_matrix(π / 8))) decomp_circuit = cirq.decompose(qd.matrix_to_cirq_circuit(U0_matrix(π / 8)), keep=keep_fuction) print(cirq.Circuit(decomp_circuit))
def _check(A): with warnings.catch_warnings(): warnings.simplefilter("ignore") assert_all_close(A, qd.matrix_to_cirq_circuit(A).unitary())