def test_applying_operators(self): """Adding Cbits and Qubits to a circuit.""" circuit = Circuit() q0 = circuit.create_qubit() c0 = circuit.create_cbit() q1 = circuit.create_qubit() circuit.apply_operator(H(), [q1]) circuit.apply_operator(X(), [q1, q0]) circuit.apply_operator(H(), [q1], [c0]) circuit.apply_operator(X(), [q1, q0], [c0])
def test_x(self): """Single uncontrolled X""" tweedledum_circuit = Circuit() tweedledum_circuit.apply_operator(X(), [tweedledum_circuit.create_qubit()]) circuit = tweedledum2qiskit(tweedledum_circuit) expected = QuantumCircuit(1) expected.x(0) self.assertEqual(circuit, expected)
def phaseflip_circuit(f: BoolFunction, method: str, config: dict() = {}): r"""Synthesizes a Boolean Function as a phaseflip oracle. A phaseflip oracle is a quantum operator `Pf` specified by a Boolean function `f` for which the effect on all computational basis states is given by Bf : |x>|0>^a --> (-1)^{f(x)}|x>|0>^a Note that you can easily construct a phase oracle from a bit oracle by sandwiching the controlled X gate on the result qubit by a X and H gate. """ synthesizer = _METHOD_TO_CALLABLE.get(method) if synthesizer is None: raise ValueError(f"Unrecognized synthesis method: {method}") if method in ["pprm", "pkrm", "spectrum"]: if f.num_outputs() > 1: raise ValueError("TT based methods only work for single output functions") if f.num_inputs() > 16: raise ValueError( "TT based methods only work for functions with at most 16 inputs" ) if f"{method}_synth" not in config: config[f"{method}_synth"] = {"phase_esop": True} else: config[f"{method}_synth"]["phase_esop"] = True return synthesizer(f.truth_table(output_bit=0), config) circuit = Circuit() num_qubits = f.num_inputs() + f.num_outputs() qubits = [circuit.create_qubit() for _ in range(num_qubits)] cbits = list() for i in range(f.num_inputs(), num_qubits): circuit.apply_operator(X(), [qubits[i]]) circuit.apply_operator(H(), [qubits[i]]) synthesizer(circuit, qubits, cbits, f.logic_network(), config) for i in range(f.num_inputs(), num_qubits): circuit.apply_operator(H(), [qubits[i]]) circuit.apply_operator(X(), [qubits[i]]) return circuit
def test_cx_1_0(self): """CX(1, 0)""" tweedledum_circuit = Circuit() qubits = list() qubits.append(tweedledum_circuit.create_qubit()) qubits.append(tweedledum_circuit.create_qubit()) tweedledum_circuit.apply_operator(X(), [qubits[1], qubits[0]]) circuit = tweedledum2qiskit(tweedledum_circuit) expected = QuantumCircuit(2) expected.append(XGate().control(1, ctrl_state="1"), [1, 0]) self.assertEqual(expected, circuit)
def test_cx_0_1(self): """CX(0, 1)""" tweedledum_circuit = Circuit() qubits = list() qubits.append(tweedledum_circuit.create_qubit()) qubits.append(tweedledum_circuit.create_qubit()) tweedledum_circuit.apply_operator(X(), [qubits[0], qubits[1]]) circuit = tweedledum2qiskit(tweedledum_circuit) expected = QuantumCircuit(2) expected.append(XGate().control(1, ctrl_state='1'), [0, 1]) self.assertEqual(circuit, expected)
def test_cx_qreg(self): """CX(0, 1) with qregs parameter""" tweedledum_circuit = Circuit() qubits = list() qubits.append(tweedledum_circuit.create_qubit()) qubits.append(tweedledum_circuit.create_qubit()) tweedledum_circuit.apply_operator(X(), [qubits[1], qubits[0]]) qr = QuantumRegister(2, "qr") circuit = tweedledum2qiskit(tweedledum_circuit, qregs=[qr]) expected = QuantumCircuit(qr) expected.append(XGate().control(1, ctrl_state="1"), [qr[1], qr[0]]) self.assertEqual(expected, circuit)